import { spawnSync } from "node:child_process"; import { readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; /** * R8 of docs/manual/03-outside-feature-folder.md: a check with nothing to * check fails. tsc passes a tsconfig whose `files` is empty when it extends * another one, so `check:types:web-worker` compiled zero files and passed * FE-GATE-003 from 2026-09-25 until it was deleted on 2026-11-08. Every * tsconfig a package script hands to tsc must list at least one file of this * repository (lib and node_modules declarations do not count). */ const TSC_PROJECT = /\btsc\s+(?:--project|-p)\s+(\S+)/gu; export function typecheckProjects(scripts: Readonly>): string[] { const projects = new Set(); for (const command of Object.values(scripts)) { for (const match of command.matchAll(TSC_PROJECT)) projects.add(match[1]!); } return [...projects].sort(); } export function ownInputFiles(listed: string): string[] { return listed .split("\n") .map((line) => line.trim()) .filter((line) => line.length > 0 && !line.split(path.sep).includes("node_modules")); } if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { const scripts = (JSON.parse(readFileSync("package.json", "utf8")) as { scripts: Record }).scripts; const projects = typecheckProjects(scripts); const counts = projects.map((project) => { const result = spawnSync(path.resolve("node_modules/.bin/tsc"), ["--project", project, "--listFilesOnly"], { encoding: "utf8", }); if (result.error) throw result.error; return { project, files: ownInputFiles(result.stdout).length }; }); const empty = counts.filter(({ files }) => files === 0); if (projects.length === 0 || empty.length > 0) { process.stderr.write( projects.length === 0 ? "Typecheck inputs: FAIL (no package script runs tsc --project)\n" : `Typecheck inputs: FAIL (no input files: ${empty.map(({ project }) => project).join(", ")})\n`, ); process.exitCode = 1; } else { process.stdout.write( `Typecheck inputs: PASS (${counts.map(({ project, files }) => `${project} ${files}`).join(", ")})\n`, ); } }