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:
@@ -16,6 +16,8 @@
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
const { ExitError, runMain } = require('./lib/cli-exit.cjs');
|
||||
|
||||
const ROOT = path.resolve(__dirname, '..');
|
||||
const MANIFEST_PATH = path.join(ROOT, 'docs', 'INVENTORY-MANIFEST.json');
|
||||
|
||||
@@ -70,40 +72,44 @@ function buildManifest() {
|
||||
return manifest;
|
||||
}
|
||||
|
||||
const [, , flag] = process.argv;
|
||||
function main() {
|
||||
const [, , flag] = process.argv;
|
||||
|
||||
if (flag === '--check') {
|
||||
const committed = JSON.parse(fs.readFileSync(MANIFEST_PATH, 'utf8'));
|
||||
const live = buildManifest();
|
||||
// Strip the generated date for comparison
|
||||
delete committed.generated;
|
||||
delete live.generated;
|
||||
const committedStr = JSON.stringify(committed, null, 2);
|
||||
const liveStr = JSON.stringify(live, null, 2);
|
||||
if (committedStr !== liveStr) {
|
||||
process.stderr.write(
|
||||
'docs/INVENTORY-MANIFEST.json is stale. Run:\n' +
|
||||
' node scripts/gen-inventory-manifest.cjs --write\n' +
|
||||
'then add a matching row in docs/INVENTORY.md for each new entry.\n\n',
|
||||
);
|
||||
// Show diff-friendly output
|
||||
for (const family of Object.keys(live.families)) {
|
||||
const liveSet = new Set(live.families[family]);
|
||||
const committedSet = new Set((committed.families || {})[family] || []);
|
||||
for (const name of liveSet) {
|
||||
if (!committedSet.has(name)) process.stderr.write(' + ' + family + '/' + name + '\n');
|
||||
}
|
||||
for (const name of committedSet) {
|
||||
if (!liveSet.has(name)) process.stderr.write(' - ' + family + '/' + name + '\n');
|
||||
if (flag === '--check') {
|
||||
const committed = JSON.parse(fs.readFileSync(MANIFEST_PATH, 'utf8'));
|
||||
const live = buildManifest();
|
||||
// Strip the generated date for comparison
|
||||
delete committed.generated;
|
||||
delete live.generated;
|
||||
const committedStr = JSON.stringify(committed, null, 2);
|
||||
const liveStr = JSON.stringify(live, null, 2);
|
||||
if (committedStr !== liveStr) {
|
||||
process.stderr.write(
|
||||
'docs/INVENTORY-MANIFEST.json is stale. Run:\n' +
|
||||
' node scripts/gen-inventory-manifest.cjs --write\n' +
|
||||
'then add a matching row in docs/INVENTORY.md for each new entry.\n\n',
|
||||
);
|
||||
// Show diff-friendly output
|
||||
for (const family of Object.keys(live.families)) {
|
||||
const liveSet = new Set(live.families[family]);
|
||||
const committedSet = new Set((committed.families || {})[family] || []);
|
||||
for (const name of liveSet) {
|
||||
if (!committedSet.has(name)) process.stderr.write(' + ' + family + '/' + name + '\n');
|
||||
}
|
||||
for (const name of committedSet) {
|
||||
if (!liveSet.has(name)) process.stderr.write(' - ' + family + '/' + name + '\n');
|
||||
}
|
||||
}
|
||||
throw new ExitError(1);
|
||||
}
|
||||
process.exit(1);
|
||||
process.stdout.write('docs/INVENTORY-MANIFEST.json is up to date.\n');
|
||||
} else if (flag === '--write') {
|
||||
const manifest = buildManifest();
|
||||
fs.writeFileSync(MANIFEST_PATH, JSON.stringify(manifest, null, 2) + '\n');
|
||||
process.stdout.write('Wrote ' + MANIFEST_PATH + '\n');
|
||||
} else {
|
||||
process.stdout.write(JSON.stringify(buildManifest(), null, 2) + '\n');
|
||||
}
|
||||
process.stdout.write('docs/INVENTORY-MANIFEST.json is up to date.\n');
|
||||
} else if (flag === '--write') {
|
||||
const manifest = buildManifest();
|
||||
fs.writeFileSync(MANIFEST_PATH, JSON.stringify(manifest, null, 2) + '\n');
|
||||
process.stdout.write('Wrote ' + MANIFEST_PATH + '\n');
|
||||
} else {
|
||||
process.stdout.write(JSON.stringify(buildManifest(), null, 2) + '\n');
|
||||
}
|
||||
|
||||
runMain(main);
|
||||
|
||||
Reference in New Issue
Block a user