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: use exit code #43

Merged
merged 1 commit into from
Jan 6, 2025
Merged
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
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
Loading