|
| 1 | +/** |
| 2 | + * TypeScript compatibility test runner. |
| 3 | + * |
| 4 | + * For every TypeScript version listed in typescript-versions.json, this script: |
| 5 | + * 1. Installs that TypeScript version in isolation (under .tmp/). |
| 6 | + * 2. Type-checks fixtures/index.ts - which imports and uses one client per |
| 7 | + * protocol - against it. The clients' published .d.ts are parsed (so |
| 8 | + * downlevel *syntax* incompatibilities surface), while skipLibCheck avoids |
| 9 | + * failing on their internal Node references (see tsconfig.json). |
| 10 | + * |
| 11 | + * Work is parallelized across a pool of worker threads sized to the number of |
| 12 | + * available processors (os.cpus().length). |
| 13 | + * |
| 14 | + * Usage: node ./run.mjs |
| 15 | + */ |
| 16 | +import { execFileSync } from "node:child_process"; |
| 17 | +import { existsSync, readFileSync } from "node:fs"; |
| 18 | +import os from "node:os"; |
| 19 | +import path from "node:path"; |
| 20 | +import { Worker } from "node:worker_threads"; |
| 21 | + |
| 22 | +const root = import.meta.dirname; |
| 23 | + |
| 24 | +// The versions under test live in typescript-versions.json (oldest -> newest) |
| 25 | +// so the support range can be updated without touching runner logic. Each entry |
| 26 | +// is { version: string, tscArgs?: string[] } where `version` is any spec npm |
| 27 | +// accepts (a bare minor like "5.4" installs its latest patch) and `tscArgs` |
| 28 | +// holds compiler options that CHANGED for that version and must be passed on |
| 29 | +// the command line (overriding tsconfig.json) so the shared base config stays |
| 30 | +// valid everywhere. Past examples of why an entry needed tscArgs: |
| 31 | +// - `moduleResolution: node` (node10) became a deprecation *error* in TS 6.0. |
| 32 | +// - `moduleResolution: node` (node10) was removed in TS 7.0; `bundler` |
| 33 | +// resolution requires `module: preserve` (or esnext). |
| 34 | +const VERSIONS_FILE = path.join(root, "typescript-versions.json"); |
| 35 | + |
| 36 | +/** |
| 37 | + * Load the version specs under test. |
| 38 | + * @returns {{ version: string, tscArgs: string[] }[]} |
| 39 | + */ |
| 40 | +function loadVersions() { |
| 41 | + /** @type {{ version: string, tscArgs?: string[] }[]} */ |
| 42 | + const specs = JSON.parse(readFileSync(VERSIONS_FILE, "utf8")); |
| 43 | + return specs.map(({ version, tscArgs = [] }) => ({ version, tscArgs })); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Ensure the workspace clients are installed and built (dist-types present). |
| 48 | + * The clients are consumed via file: links, so their .d.ts must exist on disk. |
| 49 | + */ |
| 50 | +function ensureClientsReady() { |
| 51 | + if (!existsSync(path.join(root, "node_modules"))) { |
| 52 | + console.log("Installing fixture dependencies (clients) ..."); |
| 53 | + execFileSync("npm", ["install", "--no-audit", "--no-fund"], { cwd: root, stdio: "inherit" }); |
| 54 | + } |
| 55 | + |
| 56 | + const clients = [ |
| 57 | + "@aws-sdk/client-dynamodb", |
| 58 | + "@aws-sdk/client-cloudwatch-logs", |
| 59 | + "@aws-sdk/client-sts", |
| 60 | + "@aws-sdk/client-ec2", |
| 61 | + "@aws-sdk/client-lambda", |
| 62 | + "@aws-sdk/client-s3", |
| 63 | + ]; |
| 64 | + const missing = clients.filter( |
| 65 | + (name) => !existsSync(path.join(root, "node_modules", ...name.split("/"), "dist-types", "index.d.ts")) |
| 66 | + ); |
| 67 | + if (missing.length > 0) { |
| 68 | + console.error( |
| 69 | + `\nThe following clients are not built (dist-types missing): ${missing.join(", ")}.\n` + |
| 70 | + `Build them first, e.g.:\n` + |
| 71 | + ` node ./scripts/turbo build -F=@aws-sdk/client-dynamodb -F=@aws-sdk/client-cloudwatch-logs \\\n` + |
| 72 | + ` -F=@aws-sdk/client-sts -F=@aws-sdk/client-ec2 -F=@aws-sdk/client-lambda -F=@aws-sdk/client-s3\n` |
| 73 | + ); |
| 74 | + process.exit(1); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Run all compile jobs across a worker pool. |
| 80 | + * @param {{ minor: string, version: string }[]} versions |
| 81 | + */ |
| 82 | +async function runPool(versions) { |
| 83 | + const poolSize = Math.max(1, Math.min(os.cpus().length, versions.length)); |
| 84 | + |
| 85 | + const queue = [...versions]; |
| 86 | + /** @type {{ version: string, ok: boolean, output: string }[]} */ |
| 87 | + const results = []; |
| 88 | + |
| 89 | + await new Promise((resolve, reject) => { |
| 90 | + let active = 0; |
| 91 | + |
| 92 | + const workerPath = path.join(root, "worker.mjs"); |
| 93 | + |
| 94 | + const spawnWorker = () => { |
| 95 | + const job = queue.shift(); |
| 96 | + if (!job) { |
| 97 | + return; |
| 98 | + } |
| 99 | + active++; |
| 100 | + const worker = new Worker(workerPath, { workerData: { root, job } }); |
| 101 | + |
| 102 | + worker.once("message", (res) => { |
| 103 | + results.push(res); |
| 104 | + worker.terminate(); |
| 105 | + }); |
| 106 | + worker.once("error", reject); |
| 107 | + worker.once("exit", () => { |
| 108 | + active--; |
| 109 | + if (queue.length > 0) { |
| 110 | + spawnWorker(); |
| 111 | + } else if (active === 0) { |
| 112 | + resolve(undefined); |
| 113 | + } |
| 114 | + }); |
| 115 | + }; |
| 116 | + |
| 117 | + for (let i = 0; i < poolSize; i++) { |
| 118 | + spawnWorker(); |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + return results; |
| 123 | +} |
| 124 | + |
| 125 | +ensureClientsReady(); |
| 126 | +const versions = loadVersions(); |
| 127 | +const results = await runPool(versions); |
| 128 | + |
| 129 | +results.sort( |
| 130 | + (a, b) => versions.findIndex((v) => v.version === a.version) - versions.findIndex((v) => v.version === b.version) |
| 131 | +); |
| 132 | + |
| 133 | +const failures = results.filter((r) => !r.ok); |
| 134 | + |
| 135 | +console.log("\n" + "=".repeat(60)); |
| 136 | +console.log("TypeScript compatibility summary"); |
| 137 | +console.log("=".repeat(60)); |
| 138 | +for (const r of results) { |
| 139 | + console.log(` ${r.ok ? "PASS" : "FAIL"} typescript@${r.version}`); |
| 140 | +} |
| 141 | + |
| 142 | +if (failures.length > 0) { |
| 143 | + for (const f of failures) { |
| 144 | + console.error(`\nFAIL typescript@${f.version}:\n${f.output.trim()}`); |
| 145 | + } |
| 146 | + process.exit(1); |
| 147 | +} |
0 commit comments