Part 2 of 2 of the n/no-process-exit cleanup (completes umbrella #738; part 1 was #739/scripts). Converts the 20 flagged process.exit() calls in the three hand-written gsd-core/bin CLI entrypoints and flips n/no-process-exit to error. - New src/cli-exit.cts -> gsd-core/bin/lib/cli-exit.cjs (ExitError + runMain), the gsd-core-side equivalent of scripts/lib/cli-exit.cjs; registered in .gitignore, eslint ignores, and the inventory manifest like its siblings. - gsd-tools.cjs: 13 apply-prompt-budget exits -> throw ExitError; main()->runMain. - verify-reapply-patches.cjs: 6 exits -> throw ExitError / return verdict; runMain. - check-latest-version.cjs: 1 exit -> return verdict; runMain. - eslint.config.mjs: n/no-process-exit warn -> error. Scope note: the gsd-core/bin/lib/*.cjs modules (core, state, profile-pipeline, roadmap-command-router, adr-parser, ui-safety-gate) are tsc-generated and eslint-ignored (ADR-457), so their process.exit calls were never flagged and are intentionally left untouched. Only the linted hand-written entrypoints are in scope. Exit codes verified unchanged for all three entrypoints. Closes #738 Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
41
src/cli-exit.cts
Normal file
41
src/cli-exit.cts
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Error carrying a process exit code. CLI logic throws this instead of calling
|
||||
* process.exit() (banned by n/no-process-exit); runMain() translates it into
|
||||
* process.exitCode at the entrypoint.
|
||||
*/
|
||||
class ExitError extends Error {
|
||||
code: number;
|
||||
hasUserMessage: boolean;
|
||||
constructor(code = 1, message?: string) {
|
||||
super(message === undefined ? `process exit ${code}` : message);
|
||||
this.name = 'ExitError';
|
||||
this.code = code;
|
||||
this.hasUserMessage = message !== undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a CLI main and translate its outcome into process.exitCode (never
|
||||
* process.exit, so n/no-process-exit stays satisfied; output flushes and
|
||||
* process.on('exit') cleanup still fires). main may be sync or async:
|
||||
* number return -> process.exitCode = it
|
||||
* thrown ExitError -> process.exitCode = err.code (+ stderr err.message if hasUserMessage && code!=0)
|
||||
* other throw -> stderr stack + process.exitCode = 1
|
||||
*/
|
||||
function runMain(main: () => number | void | Promise<number | void>): void {
|
||||
Promise.resolve()
|
||||
.then(() => main())
|
||||
.then((code) => { if (typeof code === 'number') process.exitCode = code; })
|
||||
.catch((err: unknown) => {
|
||||
if (err instanceof ExitError) {
|
||||
if (err.hasUserMessage && err.code !== 0) process.stderr.write(`${err.message}\n`);
|
||||
process.exitCode = err.code;
|
||||
return;
|
||||
}
|
||||
const e = err as Error;
|
||||
process.stderr.write(`${e && e.stack ? e.stack : String(err)}\n`);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
}
|
||||
|
||||
export = { ExitError, runMain };
|
||||
Reference in New Issue
Block a user