|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * sync-readme.mjs — Sync packages/governance/README.md from the repo-root README. |
| 4 | + * |
| 5 | + * Why: npm publishes the package-local README (the one inside packages/governance/), |
| 6 | + * not the repo-root one. Without sync, npm users see a stale doc. This script |
| 7 | + * runs in `prepublishOnly` so every release ships an in-sync README, and is |
| 8 | + * also enforced in CI to catch drift on PRs. |
| 9 | + * |
| 10 | + * Transforms repo-relative links into absolute GitHub URLs so they resolve |
| 11 | + * correctly when read on npmjs.com (where there's no monorepo context). |
| 12 | + * |
| 13 | + * Idempotent — running twice is a no-op. |
| 14 | + */ |
| 15 | +import { readFileSync, writeFileSync } from "node:fs"; |
| 16 | +import { fileURLToPath } from "node:url"; |
| 17 | +import { dirname, join } from "node:path"; |
| 18 | + |
| 19 | +const REPO = "lua-ai-global/governance"; |
| 20 | +const ROOT = join(dirname(fileURLToPath(import.meta.url)), ".."); |
| 21 | +const SRC = join(ROOT, "README.md"); |
| 22 | +const DST = join(ROOT, "packages", "governance", "README.md"); |
| 23 | + |
| 24 | +const root = readFileSync(SRC, "utf8"); |
| 25 | + |
| 26 | +// Map repo-relative links → absolute GitHub URLs. Order matters: more |
| 27 | +// specific patterns first so they don't get clobbered by broader ones. |
| 28 | +const transforms = [ |
| 29 | + // Directory references (use /tree/main/) |
| 30 | + [/\]\(\.\/packages\/governance\)/g, `](https://github.com/${REPO}/tree/main/packages/governance)`], |
| 31 | + [/\]\(\.\/packages\/governance-platform\)/g, `](https://github.com/${REPO}/tree/main/packages/governance-platform)`], |
| 32 | + // File references inside packages (use /blob/main/) |
| 33 | + [/\]\(\.\/packages\/([^)]+)\)/g, `](https://github.com/${REPO}/blob/main/packages/$1)`], |
| 34 | + // Top-level files |
| 35 | + [/\]\(\.\/LICENSE\)/g, `](https://github.com/${REPO}/blob/main/LICENSE)`], |
| 36 | + [/\]\(\.\/CONTRIBUTING\.md\)/g, `](https://github.com/${REPO}/blob/main/CONTRIBUTING.md)`], |
| 37 | + [/\]\(\.\/SECURITY\.md\)/g, `](https://github.com/${REPO}/blob/main/SECURITY.md)`], |
| 38 | + [/\]\(\.\/CODE_OF_CONDUCT\.md\)/g, `](https://github.com/${REPO}/blob/main/CODE_OF_CONDUCT.md)`], |
| 39 | +]; |
| 40 | + |
| 41 | +let synced = root; |
| 42 | +for (const [pattern, replacement] of transforms) { |
| 43 | + synced = synced.replace(pattern, replacement); |
| 44 | +} |
| 45 | + |
| 46 | +// Sanity check: any remaining `](./` link is suspicious — flag but don't fail. |
| 47 | +const stragglers = synced.match(/\]\(\.\/[^)]+\)/g); |
| 48 | +if (stragglers) { |
| 49 | + console.warn(`[sync-readme] WARNING: ${stragglers.length} relative link(s) remain unconverted:`); |
| 50 | + for (const s of stragglers) console.warn(` ${s}`); |
| 51 | + console.warn("[sync-readme] Add a transform rule in scripts/sync-readme.mjs if these should be absolute."); |
| 52 | +} |
| 53 | + |
| 54 | +writeFileSync(DST, synced); |
| 55 | +console.log(`[sync-readme] Wrote ${synced.length} bytes to packages/governance/README.md`); |
0 commit comments