|
| 1 | +import { readdir, unlink, writeFile } from "node:fs/promises"; |
| 2 | +import { relative } from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { spawnSync } from "node:child_process"; |
| 5 | + |
| 6 | +const dir = fileURLToPath(new URL(".", import.meta.url)); |
| 7 | +const tsc = fileURLToPath( |
| 8 | + new URL("./node_modules/typescript/bin/tsc", import.meta.url), |
| 9 | +); |
| 10 | +const config = fileURLToPath( |
| 11 | + new URL("./.diagnostic.tsconfig.json", import.meta.url), |
| 12 | +); |
| 13 | +const examples = fileURLToPath(new URL("../../examples/", import.meta.url)); |
| 14 | +const generated = fileURLToPath( |
| 15 | + new URL("./fixtures/generated/", import.meta.url), |
| 16 | +); |
| 17 | + |
| 18 | +let failed = false; |
| 19 | +const files = [ |
| 20 | + ...(await filesIn(examples)), |
| 21 | + ...(await filesIn(generated, true)), |
| 22 | +]; |
| 23 | + |
| 24 | +try { |
| 25 | + for (const file of files) { |
| 26 | + const path = relative(dir, file); |
| 27 | + await writeFile( |
| 28 | + config, |
| 29 | + JSON.stringify({ |
| 30 | + extends: "./tsconfig.json", |
| 31 | + files: [path], |
| 32 | + }), |
| 33 | + ); |
| 34 | + |
| 35 | + const result = spawnSync(process.execPath, [ |
| 36 | + tsc, |
| 37 | + "--extendedDiagnostics", |
| 38 | + "--project", |
| 39 | + config, |
| 40 | + "--pretty", |
| 41 | + "false", |
| 42 | + ], { cwd: dir, encoding: "utf8" }); |
| 43 | + |
| 44 | + const output = `${result.stdout}${result.stderr}`; |
| 45 | + const instantiations = match(output, /^Instantiations:\s+(.+)$/m); |
| 46 | + const checkTime = match(output, /^Check time:\s+(.+)$/m); |
| 47 | + const error = output.match(/^.*error TS\d+:.*$/m)?.[0]; |
| 48 | + const status = result.status === 0 ? "PASS" : "FAIL"; |
| 49 | + |
| 50 | + console.log( |
| 51 | + `${path.padEnd(34)} ${status} ` + |
| 52 | + `instantiations=${instantiations ?? "n/a"} ` + |
| 53 | + `check=${checkTime ?? "n/a"}`, |
| 54 | + ); |
| 55 | + if (error) console.log(` ${error}`); |
| 56 | + failed ||= result.status !== 0; |
| 57 | + } |
| 58 | +} finally { |
| 59 | + await unlink(config).catch(() => {}); |
| 60 | +} |
| 61 | + |
| 62 | +if (failed) process.exitCode = 1; |
| 63 | + |
| 64 | +function match(value: string, pattern: RegExp) { |
| 65 | + return value.match(pattern)?.[1]?.trim(); |
| 66 | +} |
| 67 | + |
| 68 | +async function filesIn(dir: string, optional = false): Promise<string[]> { |
| 69 | + try { |
| 70 | + return (await readdir(dir, { withFileTypes: true })) |
| 71 | + .filter((entry) => entry.isFile() && entry.name.endsWith(".ts")) |
| 72 | + .map((entry) => `${dir}${entry.name}`) |
| 73 | + .sort(); |
| 74 | + } catch (error) { |
| 75 | + if ( |
| 76 | + optional && |
| 77 | + typeof error === "object" && |
| 78 | + error !== null && |
| 79 | + "code" in error && |
| 80 | + error.code === "ENOENT" |
| 81 | + ) return []; |
| 82 | + throw error; |
| 83 | + } |
| 84 | +} |
0 commit comments