Files
msd-core/scripts/lint-descriptions.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

88 lines
2.5 KiB
JavaScript

#!/usr/bin/env node
/**
* lint-descriptions.cjs
*
* Enforces the 100-char description budget for commands/gsd/*.md files.
*
* Usage:
* node scripts/lint-descriptions.cjs [file.md ...]
*
* If no args are given, scans commands/gsd/ automatically.
* Exits 1 if any description exceeds 100 chars; exits 0 if all pass.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const { ExitError, runMain } = require('./lib/cli-exit.cjs');
const MAX_LENGTH = 100;
const COMMANDS_DIR = path.join(__dirname, '..', 'commands', 'gsd');
/**
* Parse the description field from frontmatter in a .md file.
* Returns null if no description is found.
*/
function parseDescription(content) {
const fmMatch = content.match(/^---\r?\n([\s\S]*?)\r?\n---/);
if (!fmMatch) return null;
const fm = fmMatch[1];
const quoted = fm.match(/^description:\s+"((?:[^"\\]|\\.)*)"\s*$/m);
if (quoted) return quoted[1];
const plain = fm.match(/^description:\s+(.+)$/m);
if (plain) return plain[1].trim();
return null;
}
function getFiles() {
if (process.argv.length > 2) {
return process.argv.slice(2);
}
return fs.readdirSync(COMMANDS_DIR)
.filter(f => f.endsWith('.md'))
.map(f => path.join(COMMANDS_DIR, f));
}
function main() {
const files = getFiles();
const violations = [];
for (const filePath of files) {
let content;
try {
content = fs.readFileSync(filePath, 'utf-8');
} catch (err) {
throw new ExitError(1, `ERROR: Cannot read file: ${filePath}\n ${err.message}`);
}
const description = parseDescription(content);
if (description === null) continue;
if (description.length > MAX_LENGTH) {
violations.push({ filePath, length: description.length, description });
}
}
if (violations.length === 0) {
const checked = files.length;
process.stdout.write(`ok lint-descriptions: ${checked} file(s) checked, 0 violations\n`);
return 0;
}
process.stderr.write(`\nERROR lint-descriptions: ${violations.length} violation(s) found\n\n`);
for (const v of violations) {
const preview = v.description.length > 120 ? v.description.slice(0, 117) + '...' : v.description;
process.stderr.write(` ${v.filePath}\n`);
process.stderr.write(` Length : ${v.length} (max ${MAX_LENGTH})\n`);
process.stderr.write(` Desc : ${preview}\n\n`);
}
process.stderr.write(`Trim descriptions to <= ${MAX_LENGTH} chars. Flag docs belong in argument-hint:.\n\n`);
return 1;
}
runMain(main);