import { readFile, writeFile } from "node:fs/promises"; import { fileURLToPath } from "node:url"; const root = new URL("../", import.meta.url); const metadata = {}; for (const name of ["pipelines", "cases", "smoke-lab"]) { const source = await readFile(new URL(`server/src/routes/${name}.ts`, root), "utf8"); const routes = [...source.matchAll(/router\.(get|post|put|patch|delete)\(\s*"([^"]+)"/g)]; for (const [index, route] of routes.entries()) { const handler = source.slice(route.index, routes[index + 1]?.index ?? source.length); const statuses = new Set([...handler.matchAll(/res\.status\((2\d\d)\)/g)].map(match => Number(match[1]))); if (/res\.json\(/.test(handler) || statuses.size === 0) statuses.add(200); const path = "/api" + route[2].replace(/:([A-Za-z0-9_]+)/g, "{$1}"); metadata[`${route[1].toUpperCase()} ${path}`] = { successStatuses: [...statuses].sort(), boardOnly: /assertBoard\(req\)/.test(handler), source: `server/src/routes/${name}.ts` }; } } const output = "// Generated by scripts/generate-runner-experimental-api-metadata.mjs.\nexport const experimentalApiMetadata: Record = " + JSON.stringify(metadata, null, 2) + ";\n"; const target = new URL("server/src/routes/experimental-api-metadata.ts", root); if (process.argv.includes("--check")) { if (await readFile(target, "utf8") !== output) throw new Error(`Stale experimental route metadata: ${fileURLToPath(target)}`); } else await writeFile(target, output);