refactor(scripts): replace process.exit() with ExitError + runMain handler (#739) (#740)

Part 1 of 2 of the n/no-process-exit cleanup (umbrella #738): convert every
process.exit() call in standalone scripts/** CLIs to the rule-compliant pattern.

- New shared helper scripts/lib/cli-exit.cjs: ExitError(code,message) + runMain()
  which translates a thrown ExitError / returned number into process.exitCode
  (never process.exit()), flushing output and still firing process.on('exit').
- main()-based entrypoints: throw new ExitError(code) for errors, return <code>
  for verdicts; invoked via runMain(main). Child exit codes preserved via return.
- top-level-only scripts: imperative body extracted into main() so mid-flow
  aborts (throw ExitError) actually halt; pure consts/helpers stay at module scope.
- diff-touches-shipped-paths.cjs: stdin event handling restructured to an async
  read so the whole flow runs under runMain; uncaughtException/unhandledRejection
  nets replaced by an in-band catch that preserves EXIT_ERROR=2.

Exit codes verified unchanged for every converted script (success/error/help and
the 0/1/2 semantic codes in diff-touches). Rule stays warn here; flipped to error
in part 2 (#738) once gsd-core/bin/** is also clean.

Refs #739

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-06-06 16:13:13 -04:00
committed by GitHub
parent dfbb684843
commit f729101eec
31 changed files with 1074 additions and 774 deletions

View File

@@ -28,6 +28,8 @@ const {
executionContextRefs: extractExecutionContextRefs,
} = require('./command-contract-helpers.cjs');
const { runMain } = require('./lib/cli-exit.cjs');
// ─── check one file ───────────────────────────────────────────────────────────
function check(filePath) {
@@ -79,30 +81,34 @@ function check(filePath) {
// ─── run ─────────────────────────────────────────────────────────────────────
const commandFiles = fs
.readdirSync(COMMANDS_DIR)
.filter(f => f.endsWith('.md'))
.map(f => path.join(COMMANDS_DIR, f));
function main() {
const commandFiles = fs
.readdirSync(COMMANDS_DIR)
.filter(f => f.endsWith('.md'))
.map(f => path.join(COMMANDS_DIR, f));
const results = commandFiles.map(check).filter(Boolean);
const results = commandFiles.map(check).filter(Boolean);
if (results.length === 0) {
console.log(
`ok lint-command-contract: ${commandFiles.length} command files checked, 0 violations`,
);
process.exit(0);
}
const total = results.reduce((n, r) => n + r.violations.length, 0);
process.stderr.write(
`\nERROR lint-command-contract: ${total} violation(s) across ${results.length} file(s)\n\n`,
);
for (const r of results) {
process.stderr.write(` ${r.file}\n`);
for (const v of r.violations) {
process.stderr.write(` - ${v}\n`);
if (results.length === 0) {
console.log(
`ok lint-command-contract: ${commandFiles.length} command files checked, 0 violations`,
);
return 0;
}
process.stderr.write('\n');
const total = results.reduce((n, r) => n + r.violations.length, 0);
process.stderr.write(
`\nERROR lint-command-contract: ${total} violation(s) across ${results.length} file(s)\n\n`,
);
for (const r of results) {
process.stderr.write(` ${r.file}\n`);
for (const v of r.violations) {
process.stderr.write(` - ${v}\n`);
}
process.stderr.write('\n');
}
process.stderr.write('See docs/adr/0002-command-contract-validation-module.md for the contract spec.\n\n');
return 1;
}
process.stderr.write('See docs/adr/0002-command-contract-validation-module.md for the contract spec.\n\n');
process.exit(1);
runMain(main);