Files
msd-core/scripts/lint-pr-check-project-dir.cjs
Tom Boucher f729101eec 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>
2026-06-06 16:13:13 -04:00

100 lines
2.4 KiB
JavaScript

#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const { runMain } = require('./lib/cli-exit.cjs');
const ROOT = path.join(__dirname, '..');
const DEFAULT_RELATIVE_FILES = [
'.github/workflows/test.yml',
'.github/workflows/pr-template-format.yml',
'.github/workflows/changeset-required.yml',
'scripts/lint-command-contract.cjs',
'scripts/lint-skill-deps.cjs',
'scripts/lint-descriptions.cjs',
'scripts/lint-shell-command-projection-drift.cjs',
'scripts/pr-template-policy.cjs',
'scripts/changeset/lint.cjs',
];
function defaultFiles(rootDir = ROOT) {
return DEFAULT_RELATIVE_FILES
.map((file) => path.join(rootDir, file))
.filter((file) => fs.existsSync(file));
}
function findForbiddenCwd(content, file = '<inline>') {
const findings = [];
const lines = content.split(/\r?\n/);
lines.forEach((line, index) => {
const pattern = /\bcwd\b/g;
let match;
while ((match = pattern.exec(line)) !== null) {
findings.push({
file,
line: index + 1,
column: match.index + 1,
source: line.trim(),
});
}
});
return findings;
}
function checkFiles(files, { rootDir = ROOT } = {}) {
const findings = [];
for (const file of files) {
const content = fs.readFileSync(file, 'utf8');
const rel = path.relative(rootDir, file);
findings.push(...findForbiddenCwd(content, rel));
}
return findings;
}
function formatFindings(findings) {
const lines = [
`ERROR lint-pr-check-project-dir: ${findings.length} forbidden cwd reference(s) found`,
'',
'PR checkers must use projectDir for project roots; cwd is forbidden in this layer.',
'',
];
for (const finding of findings) {
lines.push(` ${finding.file}:${finding.line}:${finding.column}`);
lines.push(` ${finding.source}`);
}
return `${lines.join('\n')}\n`;
}
function main(argv = process.argv.slice(2)) {
const files = argv.length > 0 ? argv.map((file) => path.resolve(file)) : defaultFiles();
const findings = checkFiles(files);
if (findings.length === 0) {
console.log(`ok lint-pr-check-project-dir: ${files.length} PR check files checked`);
return 0;
}
process.stderr.write(formatFindings(findings));
return 1;
}
if (require.main === module) {
runMain(main);
}
module.exports = {
DEFAULT_RELATIVE_FILES,
checkFiles,
defaultFiles,
findForbiddenCwd,
formatFindings,
main,
};