Skip to content

Commit 461c994

Browse files
committed
Extend ensureDirectoryConfirmed to display multi env error
When a command is run with multiple environment flags we cant prompt for confirmation. This commit allows `ensureDirectoryConfirmed` to accept environment and multiEnvironment params When a multi environment command run seems to be acting on an invalid directory an error is display and we do not confirm the action This matches the behavior of other instances where multi environment command actions cant be confirmed
1 parent 4d377e8 commit 461c994

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

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

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import {themeComponent, themesComponent} from './theme-ui.js'
1+
import {themeComponent, themesComponent, ensureDirectoryConfirmed} from './theme-ui.js'
22
import {Theme} from '@shopify/cli-kit/node/themes/types'
3-
import {test, describe, expect} from 'vitest'
3+
import {renderConfirmationPrompt, renderError, renderWarning} from '@shopify/cli-kit/node/ui'
4+
import {test, describe, expect, vi} from 'vitest'
5+
6+
vi.mock('@shopify/cli-kit/node/ui')
47

58
describe('themeComponent', () => {
69
test('returns the ui for a theme', async () => {
@@ -26,6 +29,46 @@ describe('themesComponent', () => {
2629
})
2730
})
2831

32+
describe('ensureDirectoryConfirmed', () => {
33+
test('should prompt for confirmation when force flag is false', async () => {
34+
vi.stubGlobal('process', {...process, stdout: {...process.stdout, isTTY: true}})
35+
vi.mocked(renderConfirmationPrompt).mockResolvedValue(true)
36+
37+
const confirmed = await ensureDirectoryConfirmed(false)
38+
39+
expect(renderWarning).toHaveBeenCalledWith({
40+
body: "It doesn't seem like you're running this command in a theme directory.",
41+
})
42+
expect(renderConfirmationPrompt).toHaveBeenCalledWith({
43+
message: 'Do you want to proceed?',
44+
})
45+
expect(confirmed).toBe(true)
46+
})
47+
48+
test('should not prompt for confirmation when called in a non-interactive environment', async () => {
49+
vi.stubGlobal('process', {...process, stdout: {...process.stdout, isTTY: false}})
50+
51+
const confirmed = await ensureDirectoryConfirmed(false)
52+
53+
expect(renderWarning).toHaveBeenCalledWith({
54+
body: "It doesn't seem like you're running this command in a theme directory.",
55+
})
56+
expect(confirmed).toBe(true)
57+
})
58+
59+
describe('during a multi environment command run', () => {
60+
test('should not prompt for confirmation and display an error', async () => {
61+
const confirmed = await ensureDirectoryConfirmed(false, undefined, 'Production', true)
62+
63+
expect(renderError).toHaveBeenCalledWith({
64+
headline: 'Environment: Production',
65+
body: "It doesn't seem like you're running this command in a theme directory.",
66+
})
67+
expect(confirmed).toBe(false)
68+
})
69+
})
70+
})
71+
2972
function theme(id: number) {
3073
return {id, name: `theme ${id}`} as Theme
3174
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {recordEvent} from '@shopify/cli-kit/node/analytics'
22
import {Theme} from '@shopify/cli-kit/node/themes/types'
33
import {LIVE_THEME_ROLE} from '@shopify/cli-kit/node/themes/utils'
4-
import {Task, renderConfirmationPrompt, renderTasks, renderWarning} from '@shopify/cli-kit/node/ui'
4+
import {Task, renderConfirmationPrompt, renderError, renderTasks, renderWarning} from '@shopify/cli-kit/node/ui'
55

66
export function themeComponent(theme: Theme) {
77
return [
@@ -21,11 +21,21 @@ export function themesComponent(themes: Theme[]) {
2121
export async function ensureDirectoryConfirmed(
2222
force: boolean,
2323
message = "It doesn't seem like you're running this command in a theme directory.",
24+
environment?: string,
25+
multiEnvironment?: boolean,
2426
) {
2527
if (force) {
2628
return true
2729
}
2830

31+
if (multiEnvironment) {
32+
renderError({
33+
headline: environment ? `Environment: ${environment}` : '',
34+
body: message,
35+
})
36+
return false
37+
}
38+
2939
renderWarning({body: message})
3040

3141
if (!process.stdout.isTTY) {

0 commit comments

Comments
 (0)