#!/usr/bin/env node// tsus CLI. Diagnostics are machine-first: stable codes, JSON mode, exact// fix edits; human text is a rendering of the same data.//// tsus check [dir] [--json]// tsus build [dir] [--json]// tsus fix [dir] [--unsafe] [--dry-run] [--code TSUSnnn] [--json]// tsus explain <CODE> [--json] | tsus explain --list// tsus init [dir] [--force]// tsus migrate <file.ts> [--stdout] [--force] [--json]import fs from "node:fs";import path from "node:path";import process from "node:process";import { checkProject, buildProject } from "../src/build.js";const USAGE = `usage: tsus check [dir] [--json] check every .tsus file under dir/src tsus build [dir] [--json] check, then compile src/ to dist/ tsus fix [dir] [--unsafe] [--dry-run] [--code TSUSnnn] [--json] apply the fixes check reports (safe ones by default) tsus explain <CODE> [--json] the rule, why it exists, bad and good examples tsus explain --list every code tsus init [dir] [--force] scaffold AGENTS.md, tsconfig.json, package.json, src/main.tsus tsus migrate <file.ts> [--stdout] [--force] [--json] convert a .ts file and write <file>.tsus next to it`;const usage = () => { console.error(USAGE); process.exit(2);};const args = process.argv.slice(2);const command = args[0];const json = args.includes("--json");const flag = (name) => args.includes(name);const option = (name) => { const i = args.indexOf(name); return i === -1 ? undefined : args[i + 1];};const positional = args.slice(1).filter((a, i, all) => !a.startsWith("--") && all[i] !== option("--code"));const dirArg = positional[0] ?? ".";const rootDir = path.resolve(dirArg);// Diagnostics produced outside the rules pipeline (macro errors) get the// same shape as the rest.const normalize = (d) => ({ explain: `tsus explain ${d.code}`, fix: null, ...d });const renderHuman = (diagnostics) => { for (const d of diagnostics) { const ts = d.tsCode ? ` [TS${d.tsCode}]` : ""; console.error(`error ${d.code}${ts}: ${d.message}`); console.error(` --> ${d.file}:${d.line}:${d.col}`); console.error(` hint: ${d.hint}`); if (d.fix) { console.error(` fix: ${d.fix.description} (${d.fix.safe ? "tsus fix" : "tsus fix --unsafe"})`); } } const fixable = diagnostics.filter((d) => d.fix?.safe).length; console.error(`${diagnostics.length} error(s)${fixable ? `, ${fixable} fixable with tsus fix` : ""}`); console.error("run tsus explain <CODE> for the rule behind any code");};const report = (result, extra) => { const diagnostics = result.diagnostics.map(normalize); const ok = diagnostics.length === 0; if (json) { console.log(JSON.stringify({ ok, diagnostics, ...extra }, null, 2)); } else if (ok) { console.log(extra?.emitted ? `built ${extra.emitted.length} file(s)` : "check passed"); } else { renderHuman(diagnostics); } process.exit(ok ? 0 : 1);};if (command === "check") { report(checkProject(rootDir));} else if (command === "build") { const result = buildProject(rootDir); report(result, { emitted: result.emitted.map((f) => path.relative(rootDir, f)) });} else if (command === "fix") { const { fixProject } = await import("../src/fix.js"); const code = option("--code"); const result = fixProject(rootDir, { unsafe: flag("--unsafe"), dryRun: flag("--dry-run"), codes: code ? new Set(code.split(",").map((c) => c.toUpperCase())) : undefined, }); const diagnostics = result.diagnostics.map(normalize); if (json) { console.log(JSON.stringify({ ok: diagnostics.length === 0, applied: result.applied, diagnostics }, null, 2)); } else { const verb = flag("--dry-run") ? "would apply" : "applied"; for (const a of result.applied) { console.log(`${verb} ${a.code} ${a.file}:${a.line}:${a.col} ${a.description}`); } console.log(`${verb} ${result.applied.length} fix(es)`); if (diagnostics.length > 0) { renderHuman(diagnostics); } } process.exit(diagnostics.length === 0 ? 0 : 1);} else if (command === "explain") { const { explain, renderExplainText, renderList } = await import("../src/explain.js"); const { REGISTRY } = await import("../src/rules/registry.js"); if (flag("--list") || positional.length === 0) { console.log(json ? JSON.stringify(REGISTRY, null, 2) : renderList()); process.exit(0); } const rule = explain(positional[0]); if (!rule) { console.error(`unknown code ${positional[0]}; tsus explain --list shows every code`); process.exit(2); } console.log(json ? JSON.stringify(rule, null, 2) : renderExplainText(rule));} else if (command === "init") { const { initProject } = await import("../src/init.js"); const result = initProject(rootDir, { force: flag("--force") }); if (json) { console.log(JSON.stringify(result, null, 2)); } else { for (const f of result.written) { console.log(`created ${path.join(dirArg, f)}`); } for (const f of result.skipped) { console.log(`kept existing ${path.join(dirArg, f)}`); } console.log("next: npm install, then npm start. Agents: read AGENTS.md first."); }} else if (command === "migrate") { const { migrateSource } = await import("../src/migrate.js"); const file = positional[0] ?? ""; if (!/\.(ts|mts)$/.test(file) || file.endsWith(".d.ts")) { console.error("usage: tsus migrate <file.ts> [--stdout] [--force] [--json]"); process.exit(2); } const input = path.resolve(file); const output = input.replace(/\.(ts|mts)$/, ".tsus"); const result = migrateSource(fs.readFileSync(input, "utf8"), path.basename(input)); const toStdout = flag("--stdout"); if (!toStdout) { if (fs.existsSync(output) && !flag("--force")) { console.error(`${path.relative(process.cwd(), output)} already exists; pass --force to overwrite`); process.exit(2); } fs.writeFileSync(output, result.code); } const summary = { input: path.relative(process.cwd(), input), output: toStdout ? "-" : path.relative(process.cwd(), output), converted: result.converted, unconverted: result.unconverted, notes: result.notes, }; if (json) { console.log(JSON.stringify(summary, null, 2)); } else { if (toStdout) { process.stdout.write(result.code); } const log = toStdout ? console.error : console.log; log(`migrated ${summary.input} -> ${summary.output}`); for (const c of result.converted) { log(` converted ${c.kind.padEnd(16)} ${c.name} (line ${c.line}): ${c.detail}`); } for (const u of result.unconverted) { const what = u.code ?? u.name; log(` todo ${u.kind.padEnd(16)} ${what} (line ${u.line}, output line ${u.outputLine}): ${u.reason}`); } for (const n of result.notes) { log(` note ${n}`); } log(`${result.converted.length} converted, ${result.unconverted.length} left to do`); } process.exit(result.unconverted.length === 0 ? 0 : 1);} else { usage();}