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

@@ -17,6 +17,7 @@
const fs = require('node:fs');
const path = require('node:path');
const { ExitError, runMain } = require('../lib/cli-exit.cjs');
const { parseFragment } = require('./parse.cjs');
const { renderChangelog } = require('./render.cjs');
const { serializeChangelog, parseChangelog } = require('./serialize.cjs');
@@ -475,29 +476,26 @@ function main() {
if (!parsed.ok) {
process.stderr.write(`${parsed.error}\n`);
process.stderr.write(usage());
process.exit(2);
throw new ExitError(2);
}
const { opts } = parsed;
if (opts.cmd !== 'render' && opts.cmd !== 'github-release-notes' && opts.cmd !== 'extract' && opts.cmd !== 'verify') {
process.stderr.write(usage());
process.exit(1);
throw new ExitError(1);
}
if (opts.cmd === 'render' && (!opts.version || !opts.date)) {
process.stderr.write('--version and --date are required for render\n');
process.exit(2);
throw new ExitError(2, '--version and --date are required for render');
}
if (opts.cmd === 'github-release-notes' && (!opts.fromRef || !opts.toRef)) {
process.stderr.write('--from and --to are required for github-release-notes\n');
process.exit(2);
throw new ExitError(2, '--from and --to are required for github-release-notes');
}
if (opts.cmd === 'extract' && (!opts.fromRef || !opts.toRef)) {
process.stderr.write('--from and --to are required for extract\n');
process.stderr.write(usage());
process.exit(1);
throw new ExitError(1);
}
if (opts.cmd === 'verify' && !opts.version) {
process.stderr.write('--version is required for verify\n');
process.exit(2);
throw new ExitError(2, '--version is required for verify');
}
if (opts.cmd === 'extract') {
@@ -509,7 +507,7 @@ function main() {
} else if (exitCode === 2) {
process.stderr.write(`no releases found in range (from=${report.from}, to=${report.to})\n`);
}
process.exit(exitCode);
return exitCode;
}
if (opts.cmd === 'verify') {
@@ -521,7 +519,7 @@ function main() {
} else {
process.stderr.write(report.error + '\n');
}
process.exit(exitCode);
return exitCode;
}
const { exitCode, report } = opts.cmd === 'render' ? cmdRender(opts) : cmdGithubReleaseNotes(opts);
@@ -541,9 +539,9 @@ function main() {
}
}
}
process.exit(exitCode);
return exitCode;
}
if (require.main === module) main();
if (require.main === module) runMain(main);
module.exports = { cmdRender, cmdExtract, cmdVerify, cmdGithubReleaseNotes, parseArgs, splitChangelog, assembleChangelog, listFragmentFiles, usage };

View File

@@ -60,6 +60,8 @@ function evaluateLint({ changedFiles, labels }) {
return { ok: false, reason: LINT_REASON.FAIL_MISSING_FRAGMENT };
}
const { ExitError, runMain } = require('../lib/cli-exit.cjs');
function main() {
const fs = require('node:fs');
const cp = require('node:child_process');
@@ -87,8 +89,7 @@ function main() {
);
changedFiles = out.split('\n').filter(Boolean);
} catch (e) {
process.stderr.write(`could not compute diff: ${e.message}\n`);
process.exit(2);
throw new ExitError(2, `could not compute diff: ${e.message}`);
}
const verdict = evaluateLint({ changedFiles, labels });
@@ -102,9 +103,9 @@ function main() {
process.stderr.write(`Run \`npm run changeset\` to create one, or add the \`${OPT_OUT_LABEL}\` label\n`);
process.stderr.write(`if this PR genuinely has no user-facing impact (test refactor, CI tweak, etc.).\n`);
}
process.exit(verdict.ok ? 0 : 1);
return verdict.ok ? 0 : 1;
}
if (require.main === module) main();
if (require.main === module) runMain(main);
module.exports = { evaluateLint, LINT_REASON, OPT_OUT_LABEL, isUserFacing, isFragment };

View File

@@ -13,6 +13,7 @@
const fs = require('node:fs');
const path = require('node:path');
const { ExitError, runMain } = require('../lib/cli-exit.cjs');
// Small word lists — keep the function simple and dependency-free.
// Together this gives ~40 * 40 * 40 = 64,000 distinct names. The lint
@@ -121,17 +122,16 @@ function main() {
if (!parsed.ok) {
process.stderr.write(`${parsed.error}\n`);
process.stderr.write('usage: changeset/new.cjs --type <Fixed|Added|...> --pr NNNN --body "..."\n');
process.exit(2);
throw new ExitError(2);
}
const { opts } = parsed;
if (!opts.type || !opts.pr || !opts.body) {
process.stderr.write('usage: changeset/new.cjs --type <Fixed|Added|...> --pr NNNN --body "..."\n');
process.exit(2);
throw new ExitError(2, 'usage: changeset/new.cjs --type <Fixed|Added|...> --pr NNNN --body "..."');
}
const file = scaffoldFragment(opts);
process.stdout.write(`${path.relative(process.cwd(), file)}\n`);
}
if (require.main === module) main();
if (require.main === module) runMain(main);
module.exports = { generateFragmentName, scaffoldFragment, parseArgs, ALLOWED_TYPES };