|
| 1 | +#!/usr/bin/env node |
| 2 | +/* |
| 3 | + * Generate <site>/pages/changelog.json for a Concord member site. |
| 4 | + * |
| 5 | + * The site changelog is derived, never hand-written: it unions the mod's |
| 6 | + * committed changelogs/<version>.md files (hand-authored or recorded by the |
| 7 | + * release pipeline) with its GitHub Releases, so it can't drift from what |
| 8 | + * actually shipped. Runs BEFORE Eleventy — the template reads pages/<slug>.json |
| 9 | + * at config-eval time, so the file must exist first. |
| 10 | + * |
| 11 | + * Opt-in: no-op unless site.json's nav includes "changelog" (that is the same |
| 12 | + * switch that puts it in the header nav). Dependency-free (Node built-in fetch). |
| 13 | + * |
| 14 | + * Policy: |
| 15 | + * - Stable releases only; prereleases (a "-suffix" version, or a release |
| 16 | + * flagged prerelease) are omitted — they stay on GitHub Releases. |
| 17 | + * - A committed changelogs/<version>.md wins over the release body. |
| 18 | + * - A stable committed version with no published release yet renders under an |
| 19 | + * "Unreleased" section. |
| 20 | + * - A release whose body is the raw GitHub auto-note format (## What's Changed |
| 21 | + * with "by @user in <pr-url>") degrades to a version + date + a link to the |
| 22 | + * GitHub release, so PR noise never reaches the player-facing page. |
| 23 | + * |
| 24 | + * Usage: node gen-changelog.cjs <site-dir> |
| 25 | + * Env: GITHUB_REPOSITORY owner/repo (falls back to parsing site.json github) |
| 26 | + * GITHUB_TOKEN optional, for API rate limits |
| 27 | + * CHANGELOG_OUT optional output path override (testing) |
| 28 | + */ |
| 29 | +const fs = require("fs"); |
| 30 | +const path = require("path"); |
| 31 | + |
| 32 | +const ACCENT = "text-accent hover:text-accent2 underline"; |
| 33 | + |
| 34 | +function readJson(p) { |
| 35 | + return JSON.parse(fs.readFileSync(p, "utf8")); |
| 36 | +} |
| 37 | + |
| 38 | +function esc(s) { |
| 39 | + return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); |
| 40 | +} |
| 41 | + |
| 42 | +// Inline Markdown -> the site's HTML span conventions. Escape first, then apply |
| 43 | +// tokens (their markers are never HTML-escaped, so this stays safe). |
| 44 | +function inline(s) { |
| 45 | + let h = esc(s); |
| 46 | + h = h.replace(/\[([^\]]+)\]\((https?:\/\/[^)]+)\)/g, |
| 47 | + (_, t, u) => `<a href='${u}' class='${ACCENT}'>${t}</a>`); |
| 48 | + h = h.replace(/\*\*([^*]+)\*\*/g, "<strong class='text-bone'>$1</strong>"); |
| 49 | + h = h.replace(/`([^`]+)`/g, "<code class='text-bone'>$1</code>"); |
| 50 | + return h; |
| 51 | +} |
| 52 | + |
| 53 | +// Detect the raw GitHub auto-generated release note format. |
| 54 | +function isRawFormat(body) { |
| 55 | + return /^#{1,3}\s+What's Changed/im.test(body) || |
| 56 | + /\bby @[\w-]+ in https?:\/\/\S+\/pull\/\d+/i.test(body) || |
| 57 | + /^\*\*Full Changelog\*\*:/im.test(body); |
| 58 | +} |
| 59 | + |
| 60 | +// Parse a clean player-facing body (lead paragraph + ## Added/Changed/... with |
| 61 | +// "- " bullets) into the template's block list. |
| 62 | +function parseBody(body) { |
| 63 | + const lines = body.replace(/\r\n/g, "\n").split("\n"); |
| 64 | + const blocks = []; |
| 65 | + const lead = []; |
| 66 | + let i = 0; |
| 67 | + for (; i < lines.length; i++) { |
| 68 | + if (/^#{1,6}\s/.test(lines[i])) break; |
| 69 | + if (lines[i].trim()) lead.push(lines[i].trim()); |
| 70 | + } |
| 71 | + if (lead.length) blocks.push({ type: "prose", html: [inline(lead.join(" "))] }); |
| 72 | + |
| 73 | + let sub = null; |
| 74 | + let list = null; |
| 75 | + const flush = () => { if (sub) { blocks.push(sub); sub = null; list = null; } }; |
| 76 | + for (; i < lines.length; i++) { |
| 77 | + const line = lines[i]; |
| 78 | + const h = line.match(/^#{1,6}\s+(.*)$/); |
| 79 | + if (h) { |
| 80 | + flush(); |
| 81 | + sub = { |
| 82 | + type: "sub", |
| 83 | + title: `<span class='text-accent uppercase tracking-wider'>${esc(h[1].trim())}</span>`, |
| 84 | + blocks: [], |
| 85 | + }; |
| 86 | + list = null; |
| 87 | + continue; |
| 88 | + } |
| 89 | + const b = line.match(/^\s*[-*]\s+(.*)$/); |
| 90 | + if (b) { |
| 91 | + if (!list) { list = { type: "list", spacing: 1, items: [] }; (sub ? sub.blocks : blocks).push(list); } |
| 92 | + list.items.push(inline(b[1].trim())); |
| 93 | + continue; |
| 94 | + } |
| 95 | + if (line.trim()) { |
| 96 | + list = null; |
| 97 | + (sub ? sub.blocks : blocks).push({ type: "prose", html: [inline(line.trim())] }); |
| 98 | + } |
| 99 | + } |
| 100 | + flush(); |
| 101 | + return blocks; |
| 102 | +} |
| 103 | + |
| 104 | +function parseVersion(v) { |
| 105 | + const m = String(v).match(/^(\d+)\.(\d+)\.(\d+)/); |
| 106 | + return m ? [+m[1], +m[2], +m[3]] : [0, 0, 0]; |
| 107 | +} |
| 108 | + |
| 109 | +function isPrerelease(version) { |
| 110 | + return version.includes("-"); |
| 111 | +} |
| 112 | + |
| 113 | +function fmtDate(iso) { |
| 114 | + return new Date(iso).toLocaleDateString("en-US", |
| 115 | + { year: "numeric", month: "short", day: "numeric", timeZone: "UTC" }); |
| 116 | +} |
| 117 | + |
| 118 | +async function fetchReleases(repo, token) { |
| 119 | + const out = []; |
| 120 | + for (let page = 1; page <= 10; page++) { |
| 121 | + const headers = { Accept: "application/vnd.github+json", "User-Agent": "concord-changelog" }; |
| 122 | + if (token) headers.Authorization = `Bearer ${token}`; |
| 123 | + const res = await fetch( |
| 124 | + `https://api.github.com/repos/${repo}/releases?per_page=100&page=${page}`, { headers }); |
| 125 | + if (!res.ok) throw new Error(`GitHub API ${res.status} for ${repo}/releases`); |
| 126 | + const batch = await res.json(); |
| 127 | + out.push(...batch); |
| 128 | + if (batch.length < 100) break; |
| 129 | + } |
| 130 | + return out; |
| 131 | +} |
| 132 | + |
| 133 | +async function main() { |
| 134 | + const siteDir = process.argv[2] && path.resolve(process.argv[2]); |
| 135 | + if (!siteDir || !fs.existsSync(path.join(siteDir, "site.json"))) { |
| 136 | + console.error("gen-changelog: <site-dir> must contain site.json"); |
| 137 | + process.exit(2); |
| 138 | + } |
| 139 | + const site = readJson(path.join(siteDir, "site.json")); |
| 140 | + if (!Array.isArray(site.nav) || !site.nav.includes("changelog")) { |
| 141 | + console.log("gen-changelog: nav has no 'changelog' — opted out, nothing to generate"); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + const repo = process.env.GITHUB_REPOSITORY || |
| 146 | + (site.github || "").replace(/^https?:\/\/github\.com\//, "").replace(/\/$/, ""); |
| 147 | + const ghBase = site.github || (repo ? `https://github.com/${repo}` : ""); |
| 148 | + |
| 149 | + // Committed changelogs/<version>.md (sibling of site/). |
| 150 | + const clDir = path.join(siteDir, "..", "changelogs"); |
| 151 | + const committed = {}; |
| 152 | + if (fs.existsSync(clDir)) { |
| 153 | + for (const f of fs.readdirSync(clDir)) { |
| 154 | + if (f.endsWith(".md")) committed[f.slice(0, -3)] = fs.readFileSync(path.join(clDir, f), "utf8"); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // GitHub Releases (best-effort — a fetch failure still renders committed files). |
| 159 | + let releases = []; |
| 160 | + if (repo) { |
| 161 | + try { |
| 162 | + releases = await fetchReleases(repo, process.env.GITHUB_TOKEN); |
| 163 | + } catch (e) { |
| 164 | + console.log(`::warning::gen-changelog: ${e.message}; rendering committed files only`); |
| 165 | + } |
| 166 | + } |
| 167 | + const relByVersion = {}; |
| 168 | + for (const r of releases) { |
| 169 | + const v = (r.tag_name || "").replace(/^v/, ""); |
| 170 | + if (v) relByVersion[v] = r; |
| 171 | + } |
| 172 | + |
| 173 | + // Union of committed + released versions, stable only. |
| 174 | + const versions = new Set([...Object.keys(committed), ...Object.keys(relByVersion)]); |
| 175 | + const entries = []; |
| 176 | + for (const v of versions) { |
| 177 | + const rel = relByVersion[v]; |
| 178 | + if (isPrerelease(v) || (rel && rel.prerelease)) continue; // stable only |
| 179 | + const body = committed[v] !== undefined ? committed[v] : (rel ? rel.body || "" : ""); |
| 180 | + entries.push({ |
| 181 | + version: v, |
| 182 | + date: rel ? rel.published_at : null, |
| 183 | + degrade: committed[v] === undefined && rel && isRawFormat(body || ""), |
| 184 | + body, |
| 185 | + }); |
| 186 | + } |
| 187 | + |
| 188 | + // Unreleased (no published release) first; then released newest-first. |
| 189 | + entries.sort((a, b) => { |
| 190 | + if (!a.date && b.date) return -1; |
| 191 | + if (a.date && !b.date) return 1; |
| 192 | + if (!a.date && !b.date) { |
| 193 | + const [A, B] = [parseVersion(b.version), parseVersion(a.version)]; |
| 194 | + return A[0] - B[0] || A[1] - B[1] || A[2] - B[2]; |
| 195 | + } |
| 196 | + return new Date(b.date) - new Date(a.date); |
| 197 | + }); |
| 198 | + |
| 199 | + const sections = entries.map((e) => { |
| 200 | + const id = "v" + e.version.replace(/\./g, "-"); |
| 201 | + const tagUrl = `${ghBase}/releases/tag/v${e.version}`; |
| 202 | + if (!e.date) { |
| 203 | + return { |
| 204 | + id: "unreleased-" + id, |
| 205 | + title: `Unreleased <span class='text-smoke'>— v${e.version}</span>`, |
| 206 | + blocks: parseBody(e.body), |
| 207 | + }; |
| 208 | + } |
| 209 | + const title = `v${e.version} <span class='text-smoke'>— ${fmtDate(e.date)}</span>`; |
| 210 | + if (e.degrade) { |
| 211 | + return { |
| 212 | + id, title, |
| 213 | + blocks: [{ type: "note", html: `Full notes on <a href='${tagUrl}' class='${ACCENT}'>GitHub Releases →</a>` }], |
| 214 | + }; |
| 215 | + } |
| 216 | + const blocks = parseBody(e.body); |
| 217 | + blocks.push({ type: "note", html: `<a href='${tagUrl}' class='${ACCENT}'>Download & full notes on GitHub →</a>` }); |
| 218 | + return { id, title, blocks }; |
| 219 | + }); |
| 220 | + |
| 221 | + sections.push({ |
| 222 | + id: "versioning", |
| 223 | + title: "Versioning", |
| 224 | + blocks: [{ |
| 225 | + type: "prose", |
| 226 | + html: [`${esc(site.name)} follows <a href='https://semver.org/' class='${ACCENT}'>semantic versioning</a>. Every tagged release is on <a href='${ghBase}/releases' class='${ACCENT}'>GitHub Releases</a>, the canonical download source; prereleases are published there but omitted from this page.`], |
| 227 | + }], |
| 228 | + }); |
| 229 | + |
| 230 | + const page = { |
| 231 | + nav: "Changelog", |
| 232 | + metaTitle: `Changelog — ${site.name}`, |
| 233 | + metaDescription: `Release history for ${site.name}: every stable version with its player-facing changes, linked to GitHub Releases.`, |
| 234 | + ogTitle: `Changelog — ${site.name}`, |
| 235 | + ogDescription: `Release history for ${site.name} with per-version changes.`, |
| 236 | + title: "Changelog", |
| 237 | + lede: `Every stable release. Jars and full notes live on <a href='${ghBase}/releases' class='${ACCENT}'>GitHub Releases</a> — the canonical download source.`, |
| 238 | + width: "4xl", |
| 239 | + sections, |
| 240 | + }; |
| 241 | + |
| 242 | + const out = process.env.CHANGELOG_OUT || path.join(siteDir, "pages", "changelog.json"); |
| 243 | + fs.mkdirSync(path.dirname(out), { recursive: true }); |
| 244 | + fs.writeFileSync(out, JSON.stringify(page, null, 2) + "\n"); |
| 245 | + console.log(`gen-changelog: wrote ${out} (${entries.length} version(s))`); |
| 246 | +} |
| 247 | + |
| 248 | +main().catch((e) => { console.error("gen-changelog:", e.message); process.exit(1); }); |
0 commit comments