Skip to content

Commit

Permalink
Render live theme confirmation on selected theme role rather than on …
Browse files Browse the repository at this point in the history
…live flag
  • Loading branch information
jamesmengo committed Apr 30, 2024
1 parent e017818 commit 3ecdbcd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
30 changes: 29 additions & 1 deletion packages/theme/src/cli/commands/theme/push.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,42 @@ describe('Push', () => {
test("renders confirmation prompt if 'allow-live' flag is not provided and selected theme role is live", async () => {
// Given
const flags: ThemeSelectionOptions = {live: true}
vi.mocked(renderConfirmationPrompt).mockResolvedValue(true)

// When
await createOrSelectTheme(adminSession, flags)
const theme = await createOrSelectTheme(adminSession, flags)

// Then
expect(theme?.role).toBe(LIVE_THEME_ROLE)
expect(renderConfirmationPrompt).toHaveBeenCalled()
})

test("renders confirmation prompt if 'allow-live' flag is not provided and live theme is specified via theme flag", async () => {
// Given
const flags: ThemeSelectionOptions = {theme: '3'}
vi.mocked(findOrSelectTheme).mockResolvedValue(buildTheme({id: 3, name: 'Live Theme', role: LIVE_THEME_ROLE})!)
vi.mocked(renderConfirmationPrompt).mockResolvedValue(true)

// When
const theme = await createOrSelectTheme(adminSession, flags)

// Then
expect(theme?.role).toBe(LIVE_THEME_ROLE)
expect(renderConfirmationPrompt).toHaveBeenCalled()
})

test('returns undefined if live theme confirmation prompt is not confirmed', async () => {
// Given
const flags: ThemeSelectionOptions = {live: true}
vi.mocked(renderConfirmationPrompt).mockResolvedValue(false)

// When
const theme = await createOrSelectTheme(adminSession, flags)

// Then
expect(theme).toBeUndefined()
})

test('renders text prompt if unpublished flag is provided and theme flag is not provided', async () => {
// Given
const flags = {unpublished: true}
Expand Down
32 changes: 19 additions & 13 deletions packages/theme/src/cli/commands/theme/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {execCLI2} from '@shopify/cli-kit/node/ruby'
import {AdminSession, ensureAuthenticatedThemes} from '@shopify/cli-kit/node/session'
import {useEmbeddedThemeCLI} from '@shopify/cli-kit/node/context/local'
import {RenderConfirmationPromptOptions, renderConfirmationPrompt} from '@shopify/cli-kit/node/ui'
import {UNPUBLISHED_THEME_ROLE, promptThemeName} from '@shopify/cli-kit/node/themes/utils'
import {LIVE_THEME_ROLE, Role, UNPUBLISHED_THEME_ROLE, promptThemeName} from '@shopify/cli-kit/node/themes/utils'
import {cwd, resolvePath} from '@shopify/cli-kit/node/path'
import {Theme} from '@shopify/cli-kit/node/themes/types'

Expand Down Expand Up @@ -220,27 +220,33 @@ export async function createOrSelectTheme(
const themeManager = new UnpublishedThemeManager(adminSession)
return themeManager.create(UNPUBLISHED_THEME_ROLE, themeName)
} else {
if (live && !flags['allow-live'] && !(await confirmPushToLiveTheme(adminSession.storeFqdn))) {
return
}
return findOrSelectTheme(adminSession, {
const selectedTheme = await findOrSelectTheme(adminSession, {
header: 'Select a theme to push to:',
filter: {
live,
theme,
},
})

if (await confirmPushToTheme(selectedTheme.role as Role, flags['allow-live'], adminSession.storeFqdn)) {
return selectedTheme
}
}
}

async function confirmPushToLiveTheme(store: string) {
const message = `Push theme files to the published theme on ${store}?`
async function confirmPushToTheme(themeRole: Role, allowLive: boolean | undefined, storeFqdn: string) {
if (themeRole === LIVE_THEME_ROLE) {
if (allowLive) {
return true
}

const options: RenderConfirmationPromptOptions = {
message,
confirmationMessage: 'Yes, confirm changes',
cancellationMessage: 'Cancel',
}
const options: RenderConfirmationPromptOptions = {
message: `Push theme files to the ${themeRole} theme on ${storeFqdn}?`,
confirmationMessage: 'Yes, confirm changes',
cancellationMessage: 'Cancel',
}

return renderConfirmationPrompt(options)
return renderConfirmationPrompt(options)
}
return true
}

0 comments on commit 3ecdbcd

Please sign in to comment.