Skip to content

Commit 95c9967

Browse files
committed
fix(theme): restore theme dev analytics by resolving backgroundJobPromise on exit
1 parent 761a464 commit 95c9967

3 files changed

Lines changed: 38 additions & 12 deletions

File tree

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ describe('createKeypressHandler', () => {
124124

125125
test('opens localhost when "t" is pressed', () => {
126126
// Given
127-
const handler = createKeypressHandler(urls, ctx)
127+
const handler = createKeypressHandler(urls, ctx, vi.fn())
128128

129129
// When
130130
handler('t', {name: 't'})
@@ -135,7 +135,7 @@ describe('createKeypressHandler', () => {
135135

136136
test('opens theme preview when "p" is pressed', () => {
137137
// Given
138-
const handler = createKeypressHandler(urls, ctx)
138+
const handler = createKeypressHandler(urls, ctx, vi.fn())
139139

140140
// When
141141
handler('p', {name: 'p'})
@@ -146,7 +146,7 @@ describe('createKeypressHandler', () => {
146146

147147
test('opens theme editor when "e" is pressed', () => {
148148
// Given
149-
const handler = createKeypressHandler(urls, ctx)
149+
const handler = createKeypressHandler(urls, ctx, vi.fn())
150150

151151
// When
152152
handler('e', {name: 'e'})
@@ -157,7 +157,7 @@ describe('createKeypressHandler', () => {
157157

158158
test('opens gift card preview when "g" is pressed', () => {
159159
// Given
160-
const handler = createKeypressHandler(urls, ctx)
160+
const handler = createKeypressHandler(urls, ctx, vi.fn())
161161

162162
// When
163163
handler('g', {name: 'g'})
@@ -169,7 +169,7 @@ describe('createKeypressHandler', () => {
169169
test('appends preview path to theme editor URL when lastRequestedPath is not "/"', () => {
170170
// Given
171171
const ctxWithPath = {lastRequestedPath: '/products/test-product'}
172-
const handler = createKeypressHandler(urls, ctxWithPath)
172+
const handler = createKeypressHandler(urls, ctxWithPath, vi.fn())
173173

174174
// When
175175
handler('e', {name: 'e'})
@@ -182,7 +182,7 @@ describe('createKeypressHandler', () => {
182182

183183
test('debounces rapid keypresses - only opens URL once during debounce window', () => {
184184
// Given
185-
const handler = createKeypressHandler(urls, ctx)
185+
const handler = createKeypressHandler(urls, ctx, vi.fn())
186186

187187
// When
188188
handler('t', {name: 't'})
@@ -197,7 +197,7 @@ describe('createKeypressHandler', () => {
197197

198198
test('allows keypresses after debounce period expires', () => {
199199
// Given
200-
const handler = createKeypressHandler(urls, ctx)
200+
const handler = createKeypressHandler(urls, ctx, vi.fn())
201201

202202
// When
203203
handler('t', {name: 't'})
@@ -220,7 +220,7 @@ describe('createKeypressHandler', () => {
220220

221221
test('debounces different keys during the same debounce window', () => {
222222
// Given
223-
const handler = createKeypressHandler(urls, ctx)
223+
const handler = createKeypressHandler(urls, ctx, vi.fn())
224224

225225
// When
226226
handler('t', {name: 't'})

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {Theme} from '@shopify/cli-kit/node/themes/types'
1313
import {checkPortAvailability, getAvailableTCPPort} from '@shopify/cli-kit/node/tcp'
1414
import {AbortError} from '@shopify/cli-kit/node/error'
1515
import {openURL} from '@shopify/cli-kit/node/system'
16+
import {postRunHookHasCompleted} from '@shopify/cli-kit/node/hooks/postrun'
1617
import {debounce} from '@shopify/cli-kit/common/function'
1718
import chalk from '@shopify/cli-kit/node/colors'
1819

@@ -128,11 +129,14 @@ export async function dev(options: DevOptions) {
128129
},
129130
}
130131

131-
const {serverStart, renderDevSetupProgress, backgroundJobPromise} = setupDevServer(options.theme, ctx)
132+
const {serverStart, renderDevSetupProgress, backgroundJobPromise, resolveBackgroundJob} = setupDevServer(
133+
options.theme,
134+
ctx,
135+
)
132136

133137
readline.emitKeypressEvents(process.stdin)
134138

135-
const keypressHandler = createKeypressHandler(urls, ctx)
139+
const keypressHandler = createKeypressHandler(urls, ctx, resolveBackgroundJob)
136140
process.stdin.on('keypress', keypressHandler)
137141

138142
await Promise.all([
@@ -149,17 +153,33 @@ export async function dev(options: DevOptions) {
149153
}
150154
}),
151155
])
156+
157+
// Wait for the post-run hook (analytics) to complete, with a 5s timeout
158+
let totalTime = 0
159+
await new Promise<void>((resolve) => {
160+
const interval = setInterval(() => {
161+
if (postRunHookHasCompleted() || totalTime > 5000) {
162+
clearInterval(interval)
163+
resolve()
164+
}
165+
totalTime += 100
166+
}, 100)
167+
})
168+
169+
process.exit(0)
152170
}
153171

154172
export function createKeypressHandler(
155173
urls: {local: string; giftCard: string; themeEditor: string; preview: string},
156174
ctx: {lastRequestedPath: string},
175+
onClose: () => void,
157176
) {
158177
const debouncedOpenURL = debounce(openURLSafely, 100, {leading: true, trailing: false})
159178

160179
return (_str: string, key: {ctrl?: boolean; name?: string}) => {
161180
if (key.ctrl && key.name === 'c') {
162-
process.exit()
181+
onClose()
182+
return
163183
}
164184

165185
switch (key.name) {
@@ -180,6 +200,7 @@ export function createKeypressHandler(
180200
case 'g':
181201
debouncedOpenURL(urls.giftCard, 'gift card preview')
182202
break
203+
case undefined:
183204
default:
184205
break
185206
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import type {Checksum, Theme} from '@shopify/cli-kit/node/themes/types'
1818
import type {DevServerContext} from './types.js'
1919

2020
export function setupDevServer(theme: Theme, ctx: DevServerContext) {
21-
const {promise: backgroundJobPromise, reject: rejectBackgroundJob} = promiseWithResolvers<never>()
21+
const {
22+
promise: backgroundJobPromise,
23+
resolve: resolveBackgroundJob,
24+
reject: rejectBackgroundJob,
25+
} = promiseWithResolvers<void>()
2226

2327
const watcherPromise = setupInMemoryTemplateWatcher(theme, ctx)
2428
const envSetup = ensureThemeEnvironmentSetup(theme, ctx, rejectBackgroundJob)
@@ -33,6 +37,7 @@ export function setupDevServer(theme: Theme, ctx: DevServerContext) {
3337
dispatchEvent: server.dispatch,
3438
renderDevSetupProgress: envSetup.renderProgress,
3539
backgroundJobPromise,
40+
resolveBackgroundJob,
3641
}
3742
}
3843

0 commit comments

Comments
 (0)