From cdb328e2b1bcee354e50f00f0861f7f8922f5222 Mon Sep 17 00:00:00 2001 From: Marty Jones Date: Thu, 19 Dec 2024 11:01:11 -0500 Subject: [PATCH] fix: use exit code Signed-off-by: Marty Jones --- package-lock.json | 4 ++-- packages/core/src/context.ts | 4 ++-- packages/core/src/index.ts | 5 ++++- packages/core/tests/application.spec.ts | 4 ++-- packages/core/tests/fakes/context.ts | 4 ---- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index a1fde35..9c4c034 100644 --- a/package-lock.json +++ b/package-lock.json @@ -67,7 +67,7 @@ "@stricli/core": "^1.0.0" }, "devDependencies": { - "@types/bun": "*" + "@types/bun": "latest" }, "peerDependencies": { "typescript": "5.6.x" @@ -80,7 +80,7 @@ "@stricli/core": "^1.0.0" }, "devDependencies": { - "@types/bun": "*" + "@types/bun": "latest" }, "peerDependencies": { "typescript": "5.6.x" diff --git a/packages/core/src/context.ts b/packages/core/src/context.ts index 69e7adf..fa1a36f 100644 --- a/packages/core/src/context.ts +++ b/packages/core/src/context.ts @@ -46,9 +46,9 @@ export interface StricliProcess extends WritableStreams { */ readonly env?: Readonly>>; /** - * Set the exit code and end the current process. + * A number which will be the process exit code. */ - readonly exit?: (code: number) => void; + exitCode?: number | string; } /** diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 6ef5225..24c5d0d 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -76,5 +76,8 @@ export async function run( context: StricliDynamicCommandContext, ): Promise { const exitCode = await runApplication(app, inputs, context); - context.process.exit?.(exitCode); + + // We set the exit code instead of calling exit() so as not + // to cancel any pending tasks (e.g. stdout writes) + context.process.exitCode = exitCode; } diff --git a/packages/core/tests/application.spec.ts b/packages/core/tests/application.spec.ts index 398728d..1afd0a6 100644 --- a/packages/core/tests/application.spec.ts +++ b/packages/core/tests/application.spec.ts @@ -94,7 +94,7 @@ function buildBasicRouteMap(brief: string) { interface ApplicationRunResult { readonly stdout: string; readonly stderr: string; - readonly exitCode: number | undefined; + readonly exitCode: number | string | undefined; } async function runWithInputs( @@ -111,7 +111,7 @@ async function runWithInputs( }; } -function serializeExitCode(exitCode: number | undefined): string { +function serializeExitCode(exitCode: number | string | undefined): string { const knownExitCode = Object.entries(ExitCode).find(([_, value]) => value === exitCode); if (knownExitCode) { return knownExitCode[0]; diff --git a/packages/core/tests/fakes/context.ts b/packages/core/tests/fakes/context.ts index a97778f..2e4c841 100644 --- a/packages/core/tests/fakes/context.ts +++ b/packages/core/tests/fakes/context.ts @@ -16,7 +16,6 @@ interface FakeProcess extends StricliProcess { readonly stdout: FakeWritable; readonly stderr: FakeWritable; readonly exit: (code: number) => void; - readonly exitCode: number; } export type FakeContext = StricliDynamicCommandContext & { @@ -60,9 +59,6 @@ export function buildFakeContext(options: FakeContextOptions = { forCommand: tru exit: (code) => { exitCode = code; }, - get exitCode() { - return exitCode; - }, }, locale: options.locale, };