Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

JavaScript · 68 lines · 2.5 KiB · at 2cc04ee

// Regenerates everything derived from the rules registry// (packages/cli/src/rules/registry.js)://   - docs/rules.md                      every code, as `tsus explain` prints it//   - the code table in AGENTS.md        (the template `tsus init` copies, and the repo copy)// `node scripts/docs.mjs --check` fails instead of writing when anything is// stale, and also fails when a TSUS4xx rule has no rejection example.import fs from "node:fs";import path from "node:path";import { fileURLToPath } from "node:url";import { REGISTRY } from "../packages/cli/src/rules/registry.js";import { renderRulesDoc, renderCodeTable } from "../packages/cli/src/explain.js";const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");const check = process.argv.includes("--check");const template = path.join(repoRoot, "packages/cli/templates/AGENTS.md");const withTable = (text) =>  text.replace(    /<!-- tsus:codes:start -->[\s\S]*?<!-- tsus:codes:end -->/,    `<!-- tsus:codes:start -->\n${renderCodeTable()}\n<!-- tsus:codes:end -->`,  );const agents = withTable(fs.readFileSync(template, "utf8"));const outputs = [  [template, agents],  [path.join(repoRoot, "AGENTS.md"), agents],  [path.join(repoRoot, "docs/rules.md"), renderRulesDoc()],];const problems = [];for (const [file, content] of outputs) {  const current = fs.existsSync(file) ? fs.readFileSync(file, "utf8") : "";  if (current === content) {    continue;  }  if (check) {    problems.push(`${path.relative(repoRoot, file)} is stale; run npm run docs`);  } else {    fs.mkdirSync(path.dirname(file), { recursive: true });    fs.writeFileSync(file, content);    console.log(`wrote ${path.relative(repoRoot, file)}`);  }}// Every strictness rule needs an example that is rejected with its code.const exampleCodes = new Set();const examplesDir = path.join(repoRoot, "examples");for (const name of fs.readdirSync(examplesDir)) {  const manifest = path.join(examplesDir, name, "example.json");  if (fs.existsSync(manifest)) {    for (const code of JSON.parse(fs.readFileSync(manifest, "utf8")).codes ?? []) {      exampleCodes.add(code);    }  }}for (const rule of REGISTRY) {  if (/^TSUS4\d\d$/.test(rule.code) && !exampleCodes.has(rule.code)) {    problems.push(`${rule.code} has no rejection example under examples/`);  }}if (problems.length > 0) {  console.error(problems.join("\n"));  process.exit(1);}if (check) {  console.log("docs are in sync with the rules registry");}