Skip to content

Commit 9a88cb6

Browse files
committed
Add ability to set theme name on push
1 parent 8b9590a commit 9a88cb6

File tree

8 files changed

+82
-7
lines changed

8 files changed

+82
-7
lines changed

.changeset/puny-plants-allow.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@shopify/cli-kit': minor
3+
'@shopify/theme': minor
4+
'@shopify/cli': minor
5+
---
6+
7+
Add ability to set theme name on push

packages/cli-kit/src/public/node/themes/theme-manager.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,28 @@ describe('ThemeManager', () => {
9292
expect(result).toEqual(mockTheme)
9393
expect(manager.getStoredThemeId()).toBe('123')
9494
})
95+
96+
test('always creates a new theme when name is provided', async () => {
97+
// Given
98+
manager.setThemeId('123')
99+
const customTheme = {...mockTheme, name: 'Custom name'}
100+
vi.mocked(themeCreate).mockResolvedValue(customTheme)
101+
102+
// When
103+
const result = await manager.findOrCreate('Custom name')
104+
105+
// Then
106+
expect(fetchTheme).not.toHaveBeenCalled()
107+
expect(themeCreate).toHaveBeenCalledWith(
108+
{
109+
name: 'Custom name',
110+
role: DEVELOPMENT_THEME_ROLE,
111+
},
112+
session,
113+
)
114+
expect(result).toEqual(customTheme)
115+
expect(manager.getStoredThemeId()).toBe('123')
116+
})
95117
})
96118

97119
describe('fetch', () => {

packages/cli-kit/src/public/node/themes/theme-manager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export abstract class ThemeManager {
1313

1414
constructor(protected adminSession: AdminSession) {}
1515

16-
async findOrCreate(): Promise<Theme> {
17-
let theme = await this.fetch()
16+
async findOrCreate(name?: string): Promise<Theme> {
17+
let theme = name ? undefined : await this.fetch()
1818
if (!theme) {
19-
theme = await this.create()
19+
theme = await this.create(undefined, name)
2020
}
2121
return theme
2222
}

packages/cli/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,6 +2330,7 @@ FLAGS
23302330
-u, --unpublished Create a new unpublished theme and push to it.
23312331
-x, --ignore=<value>... Skip uploading the specified files (Multiple flags allowed). Wrap the value in double
23322332
quotes if you're using wildcards.
2333+
--name=<value> The name for the theme. Will always create a new theme.
23332334
--no-color Disable color output.
23342335
--password=<value> Password generated from the Theme Access app.
23352336
--path=<value> The path where you want to run the command. Defaults to the current working directory.

packages/cli/oclif.manifest.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6527,6 +6527,14 @@
65276527
"name": "live",
65286528
"type": "boolean"
65296529
},
6530+
"name": {
6531+
"description": "The name for the theme. Will always create a new theme.",
6532+
"env": "SHOPIFY_FLAG_NAME",
6533+
"hasDynamicHelp": false,
6534+
"multiple": false,
6535+
"name": "name",
6536+
"type": "option"
6537+
},
65306538
"no-color": {
65316539
"allowNo": false,
65326540
"description": "Disable color output.",

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ export default class Push extends ThemeCommand {
9797
description: 'Require theme check to pass without errors before pushing. Warnings are allowed.',
9898
env: 'SHOPIFY_FLAG_STRICT_PUSH',
9999
}),
100+
name: Flags.string({
101+
description: 'The name for the theme. Will always create a new theme.',
102+
env: 'SHOPIFY_FLAG_NAME',
103+
exclusive: ['theme'],
104+
}),
100105
environment: themeFlags.environment,
101106
}
102107

packages/theme/src/cli/services/push.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,21 @@ describe('createOrSelectTheme', async () => {
258258
expect(setDevelopmentTheme).not.toHaveBeenCalled()
259259
})
260260

261+
test('creates unpublished theme when name flag is provided', async () => {
262+
// Given
263+
vi.mocked(themeCreate).mockResolvedValue(buildTheme({id: 2, name: 'Custom name', role: UNPUBLISHED_THEME_ROLE}))
264+
vi.mocked(fetchTheme).mockResolvedValue(undefined)
265+
266+
const flags: PushFlags = {name: 'Custom name'}
267+
268+
// When
269+
const theme = await createOrSelectTheme(adminSession, flags)
270+
271+
// Then
272+
expect(theme).toMatchObject({name: 'Custom name'})
273+
expect(setDevelopmentTheme).not.toHaveBeenCalled()
274+
})
275+
261276
test('creates development theme when development flag is provided', async () => {
262277
// Given
263278
vi.mocked(themeCreate).mockResolvedValue(buildTheme({id: 1, name: 'Theme', role: DEVELOPMENT_THEME_ROLE}))
@@ -272,6 +287,20 @@ describe('createOrSelectTheme', async () => {
272287
expect(setDevelopmentTheme).toHaveBeenCalled()
273288
})
274289

290+
test('creates development theme when development and name flags are provided', async () => {
291+
// Given
292+
vi.mocked(themeCreate).mockResolvedValue(buildTheme({id: 1, name: 'Custom name', role: DEVELOPMENT_THEME_ROLE}))
293+
vi.mocked(fetchTheme).mockResolvedValue(undefined)
294+
const flags: PushFlags = {development: true, name: 'Custom name'}
295+
296+
// When
297+
const theme = await createOrSelectTheme(adminSession, flags)
298+
299+
// Then
300+
expect(theme).toMatchObject({role: DEVELOPMENT_THEME_ROLE, name: 'Custom name'})
301+
expect(setDevelopmentTheme).toHaveBeenCalled()
302+
})
303+
275304
test('creates development theme when development and unpublished flags are provided', async () => {
276305
// Given
277306
vi.mocked(themeCreate).mockResolvedValue(buildTheme({id: 1, name: 'Theme', role: DEVELOPMENT_THEME_ROLE}))

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ export interface PushFlags {
108108

109109
/** The environment to push the theme to. */
110110
environment?: string[]
111+
112+
/** The name of the theme. Will always create a new theme. */
113+
name?: string
111114
}
112115

113116
/**
@@ -366,13 +369,13 @@ export async function createOrSelectTheme(
366369
flags: PushFlags,
367370
multiEnvironment?: boolean,
368371
): Promise<Theme | undefined> {
369-
const {live, development, unpublished, theme, environment} = flags
372+
const {live, development, unpublished, theme, environment, name} = flags
370373

371374
if (development) {
372375
const themeManager = new DevelopmentThemeManager(session)
373-
return themeManager.findOrCreate()
374-
} else if (unpublished) {
375-
const themeName = theme ?? (await promptThemeName('Name of the new theme'))
376+
return themeManager.findOrCreate(name)
377+
} else if (unpublished ?? name) {
378+
const themeName = name ?? theme ?? (await promptThemeName('Name of the new theme'))
376379
return themeCreate(
377380
{
378381
name: themeName,

0 commit comments

Comments
 (0)