import { execFileSync } from "node:child_process"; import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { Extractor, ExtractorConfig, ExtractorLogLevel } from "@microsoft/api-extractor"; import { messageCallback } from "./api-messages.ts"; const ROOT = join(import.meta.dirname, ".."); const PACKAGE = join(ROOT, "packages/ferrovue"); const REPORTS = join(PACKAGE, "api"); const CLI_REPORT = "cli.txt"; const check = process.argv.includes("--check"); const pkg = JSON.parse(readFileSync(join(PACKAGE, "package.json"), "utf8")) as { exports: Record }; const entries = Object.entries(pkg.exports).flatMap(([path, target]) => typeof target === "object" && target.types ? [{ name: path === "." ? "ferrovue" : path.slice(2).replaceAll("/", "-"), types: join(PACKAGE, target.types) }] : [], ); execFileSync(process.env.npm_execpath ?? "pnpm", ["--filter", "ferrovue", "run", "build"], { cwd: ROOT, stdio: ["ignore", "ignore", "inherit"] }); mkdirSync(REPORTS, { recursive: true }); const temp = mkdtempSync(join(tmpdir(), "ferrovue-api-")); const problems: string[] = []; const forgotten = new Map>(); const exported = new Map>(); for (const { name, types } of entries) { const config = ExtractorConfig.prepare({ configObject: { projectFolder: PACKAGE, mainEntryPointFilePath: types, compiler: { overrideTsconfig: { compilerOptions: { target: "ES2024", lib: ["ES2024", "DOM", "DOM.Iterable"], module: "NodeNext", moduleResolution: "NodeNext", types: ["node"], strict: true, skipLibCheck: true }, include: ["dist/**/*.d.ts"], }, }, apiReport: { enabled: true, reportFolder: REPORTS, reportTempFolder: temp, reportFileName: name }, docModel: { enabled: false }, dtsRollup: { enabled: false }, tsdocMetadata: { enabled: false }, newlineKind: "lf", messages: { compilerMessageReporting: { default: { logLevel: ExtractorLogLevel.Error } }, extractorMessageReporting: { default: { logLevel: ExtractorLogLevel.None, addToApiReportFile: true }, "ae-missing-release-tag": { logLevel: ExtractorLogLevel.None, addToApiReportFile: false }, "ae-undocumented": { logLevel: ExtractorLogLevel.None, addToApiReportFile: false }, "ae-forgotten-export": { logLevel: ExtractorLogLevel.Warning, addToApiReportFile: false }, }, tsdocMessageReporting: { default: { logLevel: ExtractorLogLevel.None, addToApiReportFile: false } }, }, }, configObjectFullPath: undefined, packageJsonFullPath: join(PACKAGE, "package.json"), }); const result = Extractor.invoke(config, { localBuild: !check, messageCallback: messageCallback((symbol) => forgotten.set(name, (forgotten.get(name) ?? new Set()).add(symbol))), }); if (result.errorCount > 0) problems.push(`${name}: API Extractor reported ${result.errorCount} errors`); if (check && result.apiReportChanged) problems.push(`${name}: the public API differs from api/${name}.api.md`); const report = readFileSync(join(temp, `${name}.api.md`), "utf8"); exported.set(name, new Set([...report.matchAll(/^export (?:declare )?(?:abstract )?(?:function|const|let|class|interface|type|enum|namespace) (\w+)/gm)].map((m) => m[1]!))); } for (const [name, symbols] of forgotten) { for (const symbol of symbols) { const home = [...exported].find(([other, names]) => other !== name && names.has(symbol))?.[0]; if (home === undefined) problems.push(`${name}: the public API names \`${symbol}\`, which no export of the package exports`); } } const help = execFileSync(process.execPath, [join(PACKAGE, "dist/cli.js"), "--help"], { encoding: "utf8" }); const reported = new Set([...entries.map((e) => `${e.name}.api.md`), CLI_REPORT]); if (check) { let committed = ""; try { committed = readFileSync(join(REPORTS, CLI_REPORT), "utf8"); } catch {} if (committed !== help) problems.push(`cli: \`ferrovue --help\` differs from api/${CLI_REPORT}`); for (const file of readdirSync(REPORTS)) if (!reported.has(file)) problems.push(`${file}: no export of the package has this report`); } else { writeFileSync(join(REPORTS, CLI_REPORT), help); for (const file of readdirSync(REPORTS)) if (!reported.has(file)) rmSync(join(REPORTS, file)); } rmSync(temp, { recursive: true, force: true }); if (problems.length) { console.error(`${problems.join("\n")}\n\nThe npm package's public API changed. If that is intended, run \`pnpm api:report\` and commit packages/ferrovue/api.`); process.exit(1); } console.log(check ? "packages/ferrovue/api is up to date" : `wrote ${reported.size} reports to packages/ferrovue/api`);