diff --git a/packages/@sanity/cli/src/cli.ts b/packages/@sanity/cli/src/cli.ts index 67bc4f130d0..b3f03881bf0 100755 --- a/packages/@sanity/cli/src/cli.ts +++ b/packages/@sanity/cli/src/cli.ts @@ -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) { - 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 { installUnhandledRejectionsHandler() @@ -77,12 +64,6 @@ export async function runCli(cliRoot: string, {cliVersion}: {cliVersion: string} env: process.env, }) - // UGLY HACK: process.exit() 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(), @@ -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 ` to `sanity help ` diff --git a/packages/@sanity/cli/test/basics.test.ts b/packages/@sanity/cli/test/basics.test.ts index b07d92f16fe..77e92b9c572 100644 --- a/packages/@sanity/cli/test/basics.test.ts +++ b/packages/@sanity/cli/test/basics.test.ts @@ -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) + }) }) })