Skip to content

Commit

Permalink
fix: use exit code
Browse files Browse the repository at this point in the history
Signed-off-by: Marty Jones <[email protected]>
  • Loading branch information
Marty Jones committed Dec 19, 2024
1 parent 4190189 commit 4230795
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export interface StricliProcess extends WritableStreams {
*/
readonly env?: Readonly<Partial<Record<string, string>>>;
/**
* 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;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,8 @@ export async function run<CONTEXT extends CommandContext>(
context: StricliDynamicCommandContext<CONTEXT>,
): Promise<void> {
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;
}
4 changes: 2 additions & 2 deletions packages/core/tests/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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];
Expand Down
4 changes: 0 additions & 4 deletions packages/core/tests/fakes/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandContext> & {
Expand Down Expand Up @@ -60,9 +59,6 @@ export function buildFakeContext(options: FakeContextOptions = { forCommand: tru
exit: (code) => {
exitCode = code;
},
get exitCode() {
return exitCode;
},
},
locale: options.locale,
};
Expand Down

0 comments on commit 4230795

Please sign in to comment.