| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * derive-changelog.mjs — emit `web/lib/changelog.generated.ts` and |
| 4 | * `web/lib/release-notes.generated.ts` from the repository CHANGELOG.md so |
| 5 | * /changelog, /changelog/<version> and the docs release-truth band render the |
| 6 | * real release record, never a hand-typed copy. |
| 7 | * |
| 8 | * Runs at `npm run prebuild`, `npm run dev` and in the vitest global setup. |
| 9 | * The output is NOT tracked (web/.gitignore): a committed copy raced between |
| 10 | * merges — two PRs that each added a CHANGELOG line each regenerated it |
| 11 | * against their own base, git merged both to a stale item count, and main |
| 12 | * went red (twice on 2026-09-26). Deriving at build time removes the race. |
| 13 | */ |
| 14 | import { readFileSync, writeFileSync, existsSync } from "node:fs"; |
| 15 | import { basename, resolve, dirname } from "node:path"; |
| 16 | import { fileURLToPath } from "node:url"; |
| 17 | import { |
| 18 | parseChangelog, |
| 19 | parseReleaseNotes, |
| 20 | renderChangelogModule, |
| 21 | renderReleaseNotesModule, |
| 22 | } from "./changelog-lib.mjs"; |
| 23 | |
| 24 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 25 | const REPO_ROOT = resolve(__dirname, "..", ".."); |
| 26 | const SOURCE = resolve(REPO_ROOT, "CHANGELOG.md"); |
| 27 | const TARGET = resolve(__dirname, "..", "lib", "changelog.generated.ts"); |
| 28 | const NOTES_TARGET = resolve(__dirname, "..", "lib", "release-notes.generated.ts"); |
| 29 | |
| 30 | if (!existsSync(SOURCE)) { |
| 31 | console.error(`[derive-changelog] CHANGELOG.md not found at ${SOURCE}`); |
| 32 | process.exit(1); |
| 33 | } |
| 34 | |
| 35 | const markdown = readFileSync(SOURCE, "utf-8"); |
| 36 | const parsed = parseChangelog(markdown); |
| 37 | const notes = parseReleaseNotes(markdown); |
| 38 | for (const [target, next, count] of [ |
| 39 | [TARGET, renderChangelogModule(parsed), parsed.releases.length], |
| 40 | [NOTES_TARGET, renderReleaseNotesModule(notes), notes.length], |
| 41 | ]) { |
| 42 | const name = `lib/${basename(target)}`; |
| 43 | const current = existsSync(target) ? readFileSync(target, "utf-8") : null; |
| 44 | if (current !== next) { |
| 45 | writeFileSync(target, next); |
| 46 | console.log(`[derive-changelog] wrote ${count} releases → ${name}`); |
| 47 | } else { |
| 48 | console.log(`[derive-changelog] ${name} is current (${count} releases)`); |
| 49 | } |
| 50 | } |
| 51 |