Skip to content

Commit 604a6bf

Browse files
Always run the Shopify command the same way as the current execution
1 parent 01d75c9 commit 604a6bf

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

packages/cli-kit/src/public/node/hooks/postrun.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {Command, Hook} from '@oclif/core'
1010
export const hook: Hook.Postrun = async ({config, Command}) => {
1111
await detectStopCommand(Command as unknown as typeof Command)
1212
await reportAnalyticsEvent({config, exitMode: 'ok'})
13-
if (!Command.hidden) fetchNotificationsInBackground()
13+
if (!Command.hidden) fetchNotificationsInBackground(Command.id)
1414
deprecationsHook(Command)
1515

1616
const command = Command.id.replace(/:/g, ' ')

packages/cli-kit/src/public/node/notifications-system.test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import {Notification, filterNotifications, showNotificationsIfNeeded} from './notifications-system.js'
1+
import {
2+
Notification,
3+
fetchNotificationsInBackground,
4+
filterNotifications,
5+
showNotificationsIfNeeded,
6+
} from './notifications-system.js'
27
import {renderError, renderInfo, renderWarning} from './ui.js'
38
import {sniffForJson} from './path.js'
9+
import {exec} from './system.js'
410
import {cacheRetrieve} from '../../private/node/conf-store.js'
511
import {afterEach, describe, expect, test, vi} from 'vitest'
612

713
vi.mock('./ui.js')
814
vi.mock('../../private/node/conf-store.js')
915
vi.mock('./path.js')
16+
vi.mock('./system.js')
1017

1118
const betweenVersins1and2: Notification = {
1219
id: 'betweenVersins1and2',
@@ -415,3 +422,29 @@ describe('showNotificationsIfNeeded', () => {
415422
expect(renderInfo).not.toHaveBeenCalled()
416423
})
417424
})
425+
426+
describe('fetchNotificationsInBackground', () => {
427+
test('calls the expected Shopify binary for global installation', async () => {
428+
// Given / When
429+
fetchNotificationsInBackground('theme:init', ['shopify', 'theme', 'init'])
430+
431+
// Then
432+
expect(exec).toHaveBeenCalledWith('shopify', ['notifications', 'list'], expect.anything())
433+
})
434+
435+
test('calls the expected Shopify binary for local installation', async () => {
436+
// Given / When
437+
fetchNotificationsInBackground('theme:init', ['npm', 'run', 'shopify', 'theme', 'init'])
438+
439+
// Then
440+
expect(exec).toHaveBeenCalledWith('npm', ['run', 'shopify', 'notifications', 'list'], expect.anything())
441+
})
442+
443+
test('calls the expected Shopify binary for dev environment', async () => {
444+
// Given / When
445+
fetchNotificationsInBackground('theme:init', ['node', 'packages/cli/bin/dev.js', 'theme', 'init'])
446+
447+
// Then
448+
expect(exec).toHaveBeenCalledWith('node', ['packages/cli/bin/dev.js', 'notifications', 'list'], expect.anything())
449+
})
450+
})

packages/cli-kit/src/public/node/notifications-system.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,22 @@ async function cacheNotifications(notifications: string): Promise<void> {
149149

150150
/**
151151
* Fetch notifications in background as a detached process.
152+
*
153+
* @param currentCommand - The current Shopify command being run.
154+
* @param argv - The arguments passed to the current process.
152155
*/
153-
export function fetchNotificationsInBackground(): void {
156+
export function fetchNotificationsInBackground(currentCommand: string, argv = process.argv): void {
157+
let command = 'shopify'
158+
const args = ['notifications', 'list']
159+
// Run the Shopify command the same way as the current execution when it's not the global installation
160+
if (argv[0] && argv[0] !== 'shopify') {
161+
command = argv[0]
162+
const indexValue = currentCommand.split(':')[0] ?? ''
163+
const index = argv.indexOf(indexValue)
164+
if (index > 0) args.unshift(...argv.slice(1, index))
165+
}
154166
// eslint-disable-next-line no-void
155-
void exec('shopify', ['notifications', 'list'], {
156-
background: true,
157-
env: {...process.env, SHOPIFY_CLI_NO_ANALYTICS: '1'},
158-
})
167+
void exec(command, args, {background: true, env: {...process.env, SHOPIFY_CLI_NO_ANALYTICS: '1'}})
159168
}
160169

161170
/**

0 commit comments

Comments
 (0)