Skip to content

Commit 9f3e231

Browse files
authored
Merge pull request #6363 from Shopify/jb-multi-env-init
Refactor theme init command to use ThemeCommand's run method
2 parents 8eb9a36 + f562355 commit 9f3e231

File tree

7 files changed

+34
-10
lines changed

7 files changed

+34
-10
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/theme': minor
3+
---
4+
5+
Extend ThemeCommand's command method to accept both flags and arguments from CLI

packages/theme/src/cli/commands/theme/init.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import {generateRandomNameForSubdirectory} from '@shopify/cli-kit/node/fs'
1313
import {renderTextPrompt} from '@shopify/cli-kit/node/ui'
1414
import {joinPath} from '@shopify/cli-kit/node/path'
1515
import {terminalSupportsPrompting} from '@shopify/cli-kit/node/system'
16+
import {InferredArgs, InferredFlags} from '@oclif/core/interfaces'
17+
import {AdminSession} from '@shopify/cli-kit/node/session'
18+
19+
type InitFlags = InferredFlags<typeof Init.flags>
20+
type InitArgs = InferredArgs<typeof Init.args>
1621

1722
export default class Init extends ThemeCommand {
1823
static summary = 'Clones a Git repository to use as a starting point for building a new theme.'
@@ -52,8 +57,7 @@ export default class Init extends ThemeCommand {
5257
}),
5358
}
5459

55-
async run(): Promise<void> {
56-
const {args, flags} = await this.parse(Init)
60+
async command(flags: InitFlags, _adminSession: AdminSession, _multiEnvironment: boolean, args: InitArgs) {
5761
const name = args.name || (await this.promptName(flags.path))
5862
const repoUrl = flags['clone-url']
5963
const destination = joinPath(flags.path, name)

packages/theme/src/cli/commands/theme/pull.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {Flags} from '@oclif/core'
66
import {recordTiming} from '@shopify/cli-kit/node/analytics'
77
import {InferredFlags} from '@oclif/core/interfaces'
88
import {AdminSession} from '@shopify/cli-kit/node/session'
9+
import {ArgOutput} from '@shopify/cli-kit/node/base-command'
910
import {Writable} from 'stream'
1011

1112
type PullFlags = InferredFlags<typeof Pull.flags>
@@ -56,6 +57,7 @@ If no theme is specified, then you're prompted to select the theme to pull from
5657
flags: PullFlags,
5758
adminSession?: AdminSession,
5859
multiEnvironment?: boolean,
60+
_args?: ArgOutput,
5961
context?: {stdout?: Writable; stderr?: Writable},
6062
) {
6163
recordTiming('theme-command:pull')

packages/theme/src/cli/commands/theme/push.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {globalFlags, jsonFlag} from '@shopify/cli-kit/node/cli'
66
import {recordTiming} from '@shopify/cli-kit/node/analytics'
77
import {AdminSession} from '@shopify/cli-kit/node/session'
88
import {InferredFlags} from '@oclif/core/interfaces'
9+
import {ArgOutput} from '@shopify/cli-kit/node/base-command'
910
import {Writable} from 'stream'
1011

1112
type PushFlags = InferredFlags<typeof Push.flags>
@@ -105,6 +106,7 @@ export default class Push extends ThemeCommand {
105106
flags: PushFlags,
106107
adminSession: AdminSession,
107108
multiEnvironment: boolean,
109+
_args?: ArgOutput,
108110
context?: {stdout?: Writable; stderr?: Writable},
109111
) {
110112
recordTiming('theme-command:push')

packages/theme/src/cli/commands/theme/share.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {getRandomName} from '@shopify/cli-kit/common/string'
77
import {recordTiming} from '@shopify/cli-kit/node/analytics'
88
import {InferredFlags} from '@oclif/core/interfaces'
99
import {AdminSession} from '@shopify/cli-kit/node/session'
10+
import {ArgOutput} from '@shopify/cli-kit/node/base-command'
1011
import {Writable} from 'stream'
1112

1213
type ShareFlags = InferredFlags<typeof Share.flags>
@@ -36,6 +37,7 @@ export default class Share extends ThemeCommand {
3637
flags: ShareFlags,
3738
adminSession: AdminSession,
3839
multiEnvironment: boolean,
40+
_args?: ArgOutput,
3941
context?: {stdout?: Writable; stderr?: Writable},
4042
) {
4143
const pushFlags: PushFlags = {

packages/theme/src/cli/utilities/theme-command.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ class TestThemeCommand extends ThemeCommand {
4343

4444
static multiEnvironmentsFlags: RequiredFlags = ['store']
4545

46-
commandCalls: {flags: any; session: AdminSession; multiEnvironment?: boolean; context?: any}[] = []
46+
commandCalls: {flags: any; session: AdminSession; multiEnvironment: boolean; args: any; context?: any}[] = []
4747

4848
async command(
4949
flags: any,
5050
session: AdminSession,
51-
multiEnvironment?: boolean,
51+
multiEnvironment = false,
52+
args?: any,
5253
context?: {stdout?: Writable; stderr?: Writable},
5354
): Promise<void> {
54-
this.commandCalls.push({flags, session, multiEnvironment, context})
55+
this.commandCalls.push({flags, session, multiEnvironment, args, context})
5556

5657
if (flags.environment && flags.environment[0] === 'command-error') {
5758
throw new Error('Mocking a command error')
@@ -108,15 +109,16 @@ class TestUnauthenticatedThemeCommand extends ThemeCommand {
108109

109110
static multiEnvironmentsFlags: RequiredFlags = ['store']
110111

111-
commandCalls: {flags: any; session: AdminSession; multiEnvironment?: boolean; context?: any}[] = []
112+
commandCalls: {flags: any; session: AdminSession; multiEnvironment?: boolean; args?: any; context?: any}[] = []
112113

113114
async command(
114115
flags: any,
115116
session: AdminSession,
116117
multiEnvironment?: boolean,
118+
args?: any,
117119
context?: {stdout?: Writable; stderr?: Writable},
118120
): Promise<void> {
119-
this.commandCalls.push({flags, session, multiEnvironment, context})
121+
this.commandCalls.push({flags, session, multiEnvironment, args, context})
120122
}
121123
}
122124

@@ -153,6 +155,8 @@ describe('ThemeCommand', () => {
153155
expect(command.commandCalls[0]).toMatchObject({
154156
flags: {environment: []},
155157
session: mockSession,
158+
multiEnvironment: false,
159+
args: {},
156160
context: undefined,
157161
})
158162
})
@@ -181,6 +185,8 @@ describe('ThemeCommand', () => {
181185
store: 'env-store.myshopify.com',
182186
},
183187
session: mockSession,
188+
multiEnvironment: false,
189+
args: {},
184190
context: undefined,
185191
})
186192
})

packages/theme/src/cli/utilities/theme-command.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export default abstract class ThemeCommand extends Command {
5656
_flags: FlagValues,
5757
_session?: AdminSession,
5858
_multiEnvironment = false,
59+
_args?: ArgOutput,
5960
_context?: {stdout?: Writable; stderr?: Writable},
6061
): Promise<void> {}
6162

@@ -68,10 +69,12 @@ export default abstract class ThemeCommand extends Command {
6869
const klass = this.constructor as unknown as Input<TFlags, TGlobalFlags, TArgs> & {
6970
multiEnvironmentsFlags: RequiredFlags
7071
flags: FlagOutput
72+
args: ArgOutput
7173
}
7274
const requiredFlags = klass.multiEnvironmentsFlags
73-
const {flags} = await this.parse(klass)
75+
const {args, flags} = await this.parse(klass)
7476
const commandRequiresAuth = 'password' in klass.flags
77+
7578
const environments = (Array.isArray(flags.environment) ? flags.environment : [flags.environment]).filter(Boolean)
7679

7780
// Single environment or no environment
@@ -85,7 +88,7 @@ export default abstract class ThemeCommand extends Command {
8588
throw new AbortError(`Path does not exist: ${flags.path}`)
8689
}
8790

88-
await this.command(flags, session)
91+
await this.command(flags, session, false, args)
8992
await this.logAnalyticsData(session)
9093
return
9194
}
@@ -264,7 +267,7 @@ export default abstract class ThemeCommand extends Command {
264267
const commandName = this.constructor.name.toLowerCase()
265268
recordEvent(`theme-command:${commandName}:multi-env:authenticated`)
266269

267-
await this.command(flags, session, true, {stdout, stderr})
270+
await this.command(flags, session, true, {}, {stdout, stderr})
268271
await this.logAnalyticsData(session)
269272
})
270273

0 commit comments

Comments
 (0)