| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * derive-facts.mjs — extract mechanical facts from the parent repo and write |
| 4 | * them as a typed TS module. Run as `prebuild`. The same logic also runs in |
| 5 | * the content-drift cron against raw.githubusercontent.com so the deployed |
| 6 | * worker can detect repo→site drift between deploys. |
| 7 | * |
| 8 | * This script delegates all derivation logic to facts-lib.mjs so that |
| 9 | * check-facts.mjs can reuse the same code for the CI drift gate. |
| 10 | */ |
| 11 | import { writeFileSync, readFileSync, existsSync } from "node:fs"; |
| 12 | import { dirname, resolve } from "node:path"; |
| 13 | import { fileURLToPath } from "node:url"; |
| 14 | import { buildFacts } from "./facts-lib.mjs"; |
| 15 | |
| 16 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 17 | const target = resolve(__dirname, "..", "lib", "facts.generated.ts"); |
| 18 | |
| 19 | const out = buildFacts(); |
| 20 | |
| 21 | // Preserve the committed `generatedAt` when every *checked* fact is unchanged, |
| 22 | // so a clean rebuild doesn't dirty the tracked file on every run. The drift |
| 23 | // gate (check-facts.mjs) ignores generatedAt + exact-build provenance; we |
| 24 | // mirror that volatile set here. Only when a checked fact changes do we stamp |
| 25 | // a fresh time. |
| 26 | const VOLATILE = new Set(["generatedAt", "sourceRevision", "sourceCommittedAt"]); |
| 27 | function readCommittedFacts() { |
| 28 | if (!existsSync(target)) return null; |
| 29 | const src = readFileSync(target, "utf-8"); |
| 30 | const m = src.match(/export const FACTS\s*:\s*\w+\s*=\s*([\s\S]*?);?\s*$/); |
| 31 | if (!m) return null; |
| 32 | try { |
| 33 | return JSON.parse(m[1]); |
| 34 | } catch { |
| 35 | return null; |
| 36 | } |
| 37 | } |
| 38 | const committed = readCommittedFacts(); |
| 39 | if (committed && typeof committed.generatedAt === "string") { |
| 40 | const sameChecked = Object.keys(out).every( |
| 41 | (k) => VOLATILE.has(k) || JSON.stringify(out[k]) === JSON.stringify(committed[k]), |
| 42 | ); |
| 43 | if (sameChecked) out.generatedAt = committed.generatedAt; |
| 44 | } |
| 45 | |
| 46 | const missing = Object.entries(out).filter( |
| 47 | ([k, v]) => |
| 48 | k !== "generatedAt" && |
| 49 | k !== "sourceRevision" && |
| 50 | k !== "sourceCommittedAt" && |
| 51 | (v == null || (Array.isArray(v) && v.length === 0)), |
| 52 | ); |
| 53 | if (missing.length > 0) { |
| 54 | console.warn("[derive-facts] missing values:", missing.map(([k]) => k).join(", ")); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | const ts = `// AUTO-GENERATED by web/scripts/derive-facts.mjs at prebuild. |
| 59 | // DO NOT EDIT — re-run \`npm run prebuild\` (or just \`npm run build\`) after changing the parent repo. |
| 60 | // Runtime KV snapshots must use this shape and include exact source provenance. |
| 61 | |
| 62 | export interface ProviderFact { id: string; label: string; env: string } |
| 63 | |
| 64 | export interface PublishedReleaseFact { |
| 65 | tag: string; |
| 66 | version: string; |
| 67 | publishedAt: string; |
| 68 | url: string; |
| 69 | } |
| 70 | |
| 71 | export interface RepoFacts { |
| 72 | generatedAt: string; |
| 73 | sourceRevision: string | null; |
| 74 | sourceCommittedAt: string | null; |
| 75 | version: string | null; |
| 76 | crates: string[]; |
| 77 | sandboxBackends: string[]; |
| 78 | providers: ProviderFact[]; |
| 79 | defaultModel: string | null; |
| 80 | nodeEngines: string | null; |
| 81 | toolCount: number | null; |
| 82 | license: string | null; |
| 83 | latestPublishedRelease: PublishedReleaseFact | null; |
| 84 | } |
| 85 | |
| 86 | export const FACTS: RepoFacts = ${JSON.stringify(out, null, 2)}; |
| 87 | `; |
| 88 | |
| 89 | writeFileSync(target, ts); |
| 90 | console.log(`[derive-facts] wrote ${target}`); |
| 91 | console.log( |
| 92 | `[derive-facts] version=${out.version} crates=${out.crates.length} ` + |
| 93 | `providers=${out.providers.length} sandboxes=${out.sandboxBackends.length} ` + |
| 94 | `default-model=${out.defaultModel} node=${out.nodeEngines} ` + |
| 95 | `tools=${out.toolCount} license=${out.license}`, |
| 96 | ); |
| 97 |