From 89c8caf15198e765175cb3d9d1d00ff7b4b9b68b Mon Sep 17 00:00:00 2001 From: Eoin Falconer Date: Wed, 18 Dec 2024 20:51:50 +0100 Subject: [PATCH 1/6] fix: enforce process.exit --- packages/@sanity/cli/src/cli.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/@sanity/cli/src/cli.ts b/packages/@sanity/cli/src/cli.ts index 67bc4f130d0..2c5d3ace029 100755 --- a/packages/@sanity/cli/src/cli.ts +++ b/packages/@sanity/cli/src/cli.ts @@ -110,6 +110,8 @@ export async function runCli(cliRoot: string, {cliVersion}: {cliVersion: string} if (core.v || core.version) { console.log(`${pkg.name} version ${pkg.version}`) 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 ` From def24710ae0e74d1bce5eb111922e903fd08640f Mon Sep 17 00:00:00 2001 From: Eoin Falconer Date: Wed, 18 Dec 2024 21:25:38 +0100 Subject: [PATCH 2/6] fix: don't monkey patch process.env --- packages/@sanity/cli/src/cli.ts | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/packages/@sanity/cli/src/cli.ts b/packages/@sanity/cli/src/cli.ts index 2c5d3ace029..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,6 +90,7 @@ 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 From b7746e67e6bb8ac484d6502430b0d619426f5624 Mon Sep 17 00:00:00 2001 From: Eoin Falconer Date: Wed, 18 Dec 2024 21:42:56 +0100 Subject: [PATCH 3/6] test: add a test --- packages/@sanity/cli/test/basics.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/@sanity/cli/test/basics.test.ts b/packages/@sanity/cli/test/basics.test.ts index b07d92f16fe..5c9f8f8ff12 100644 --- a/packages/@sanity/cli/test/basics.test.ts +++ b/packages/@sanity/cli/test/basics.test.ts @@ -2,6 +2,7 @@ import path from 'node:path' import {describe, expect} from 'vitest' +import {generateCommandsDocumentation} from '../src/util/generateCommandsDocumentation' import {describeCliTest, testConcurrent} from './shared/describe' import {getCliUserEmail, runSanityCmdCommand, studioVersions} from './shared/environment' @@ -47,5 +48,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']) + // Can just check that the template isn't there + expect(result.stdout).not.toContain(generateCommandsDocumentation({})) + expect(result.code).toBe(0) + }) }) }) From 464858968dde1a3238cf450c4925af0d38beae7e Mon Sep 17 00:00:00 2001 From: Eoin Falconer Date: Wed, 18 Dec 2024 22:10:16 +0100 Subject: [PATCH 4/6] test: fix the test --- packages/@sanity/cli/test/basics.test.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/@sanity/cli/test/basics.test.ts b/packages/@sanity/cli/test/basics.test.ts index 5c9f8f8ff12..8e1f3e60ba3 100644 --- a/packages/@sanity/cli/test/basics.test.ts +++ b/packages/@sanity/cli/test/basics.test.ts @@ -2,7 +2,6 @@ import path from 'node:path' import {describe, expect} from 'vitest' -import {generateCommandsDocumentation} from '../src/util/generateCommandsDocumentation' import {describeCliTest, testConcurrent} from './shared/describe' import {getCliUserEmail, runSanityCmdCommand, studioVersions} from './shared/environment' @@ -51,8 +50,8 @@ describeCliTest('CLI: basic commands', () => { testConcurrent('sanity --version', async () => { const result = await runSanityCmdCommand(version, ['--version']) - // Can just check that the template isn't there - expect(result.stdout).not.toContain(generateCommandsDocumentation({})) + // Just check that the result is only one line + expect(result.stdout.split('\n')).toHaveLength(1) expect(result.code).toBe(0) }) }) From 6a58620682d13c796225fc63fa25113737598f63 Mon Sep 17 00:00:00 2001 From: Eoin Falconer Date: Wed, 18 Dec 2024 22:11:03 +0100 Subject: [PATCH 5/6] fix: comment --- packages/@sanity/cli/test/basics.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@sanity/cli/test/basics.test.ts b/packages/@sanity/cli/test/basics.test.ts index 8e1f3e60ba3..7faf7f48b58 100644 --- a/packages/@sanity/cli/test/basics.test.ts +++ b/packages/@sanity/cli/test/basics.test.ts @@ -50,7 +50,7 @@ describeCliTest('CLI: basic commands', () => { testConcurrent('sanity --version', async () => { const result = await runSanityCmdCommand(version, ['--version']) - // Just check that the result is only one line + // If the help text isn't returned, the result should only be one line expect(result.stdout.split('\n')).toHaveLength(1) expect(result.code).toBe(0) }) From c7fba38a4c291cf5cf8136084daf69f10bde8e5c Mon Sep 17 00:00:00 2001 From: Eoin Falconer Date: Wed, 18 Dec 2024 22:20:20 +0100 Subject: [PATCH 6/6] test: fix a test --- packages/@sanity/cli/test/basics.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/@sanity/cli/test/basics.test.ts b/packages/@sanity/cli/test/basics.test.ts index 7faf7f48b58..77e92b9c572 100644 --- a/packages/@sanity/cli/test/basics.test.ts +++ b/packages/@sanity/cli/test/basics.test.ts @@ -50,8 +50,8 @@ describeCliTest('CLI: basic commands', () => { testConcurrent('sanity --version', async () => { const result = await runSanityCmdCommand(version, ['--version']) - // If the help text isn't returned, the result should only be one line - expect(result.stdout.split('\n')).toHaveLength(1) + // 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) }) })