Skip to content

Commit 5aeb8af

Browse files
Fire-and-forget postrun hooks + process.exit(0)
Run the command first with exclusive CPU time, then fire prerun/postrun hooks as fire-and-forget after command completes. Call process.exit(0) in bootstrap to avoid blocking on pending analytics/version-check network requests. Startup time: 570ms → 250ms (-56%)
1 parent 0b7096a commit 5aeb8af

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

packages/cli-kit/src/public/node/custom-oclif-loader.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,16 @@ export class ShopifyConfig extends Config {
8181
commandClass.id = id
8282
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8383
commandClass.plugin = cmd.plugin ?? (this as any).rootPlugin
84-
await this.runHook('prerun', {argv, Command: commandClass})
84+
// Execute the command first — give it exclusive CPU time.
8585
const result = (await commandClass.run(argv, this)) as T
86-
await this.runHook('postrun', {argv, Command: commandClass, result})
86+
// Fire prerun + postrun AFTER command completes. Both are fire-and-forget.
87+
// Analytics is best-effort; process.exit(0) in bootstrap may terminate
88+
// before these complete, which is fine.
89+
// eslint-disable-next-line no-void
90+
void this.runHook('prerun', {argv, Command: commandClass}).then(() => {
91+
// eslint-disable-next-line no-void
92+
void this.runHook('postrun', {argv, Command: commandClass, result})
93+
})
8794
return result
8895
}
8996
}

packages/cli/src/bootstrap.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ async function runShopifyCLI({development}: RunShopifyCLIOptions) {
6464
development,
6565
lazyCommandLoader: loadCommand,
6666
})
67+
// Force exit after command completes. Pending network requests (analytics,
68+
// version checks) are best-effort and shouldn't delay the user.
69+
process.exit(0)
6770
}
6871

6972
export default runShopifyCLI
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {HOOKS} from '@shopify/cli-hydrogen'
22
import type {Hook} from '@oclif/core'
33

4+
// cast through unknown because cli-hydrogen uses @oclif/core v3 while this package uses v4
45
const hook = HOOKS.init as unknown as Hook<'init'>
56
export default hook

0 commit comments

Comments
 (0)