Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle async process.exit #8103

Draft
wants to merge 6 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions packages/@sanity/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@ import {runUpdateCheck} from './util/updateNotifier'
const sanityEnv = process.env.SANITY_INTERNAL_ENV || 'production' // eslint-disable-line no-process-env
const knownEnvs = ['development', 'staging', 'production']

function wait(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}

function installProcessExitHack(finalTask: () => Promise<unknown>) {
const originalProcessExit = process.exit

// @ts-expect-error ignore TS2534
process.exit = (exitCode?: number | undefined): never => {
finalTask().finally(() => originalProcessExit(exitCode))
}
}

export async function runCli(cliRoot: string, {cliVersion}: {cliVersion: string}): Promise<void> {
installUnhandledRejectionsHandler()

Expand Down Expand Up @@ -77,12 +64,6 @@ export async function runCli(cliRoot: string, {cliVersion}: {cliVersion: string}
env: process.env,
})

// UGLY HACK: process.exit(<code>) causes abrupt exit, we want to flush telemetry before exiting
installProcessExitHack(() =>
// When process.exit() is called, flush telemetry events first, but wait no more than x amount of ms before exiting process
Promise.race([wait(2000), flushTelemetry()]),
)

telemetry.updateUserProperties({
runtimeVersion: process.version,
runtime: detectRuntime(),
Expand All @@ -109,7 +90,10 @@ export async function runCli(cliRoot: string, {cliVersion}: {cliVersion: string}

if (core.v || core.version) {
console.log(`${pkg.name} version ${pkg.version}`)
await flushTelemetry()
process.exit()
// As the process.exit function is monkey patched above and is async, the return is necessary
return
}

// Translate `sanity -h <command>` to `sanity help <command>`
Expand Down
7 changes: 7 additions & 0 deletions packages/@sanity/cli/test/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,12 @@ describeCliTest('CLI: basic commands', () => {
expect(result.stdout).toContain('CLI Developers') // name of CI user
expect(result.code).toBe(0)
})

testConcurrent('sanity --version', async () => {
const result = await runSanityCmdCommand(version, ['--version'])
// If the help text isn't returned, the result should only be one line with text
expect(result.stdout.split('\n').filter((l) => l.length > 0)).toHaveLength(1)
expect(result.code).toBe(0)
})
})
})
Loading