// Builds the site into dist/: src/index.html with the README's parity table and // the community links and contributors baked in, plus public/ and fingerprinted CSS and JS. No dependencies. // // Railway builds only website/, so the README and links.json come from the repo when they // are next to it (a local checkout) and from main on GitHub otherwise. The latest releases and // the contributors are baked in when GitHub answers, so the page reads well before (or without) JavaScript. import { readFile, writeFile, mkdir, rm, cp } from "node:fs/promises"; import { existsSync } from "node:fs"; import { createHash } from "node:crypto"; import { join, dirname } from "node:path"; import { fileURLToPath } from "node:url"; import { markdown, inline, section, escape } from "./src/markdown.js"; import { releasesHtml } from "./src/releases.js"; import { contributorsFrom, contributorsHtml } from "./src/contributors.js"; const site = dirname(fileURLToPath(import.meta.url)); const dist = join(site, "dist"); const REPO = "ludovic111/horizon"; const RAW = `https://raw.githubusercontent.com/${REPO}/main/`; const DEFAULT_LINKS = { discord: "https://discord.gg/HcTeMZxe5E", sponsor: "https://github.com/sponsors/ludovic111" }; async function repoFile(path) { const local = join(site, "..", path); if (existsSync(local)) return [await readFile(local, "utf8"), "the checkout"]; const res = await fetch(RAW + path, { signal: AbortSignal.timeout(15000) }); if (!res.ok) throw new Error(`${path}: ${res.status} from GitHub`); return [await res.text(), "main on GitHub"]; } // The README: required, a site with a stale or missing parity table is worse than no deploy. const [readme, readmeFrom] = await repoFile("README.md"); let links = { ...DEFAULT_LINKS }; try { const parsed = JSON.parse((await repoFile("launcher/links.json"))[0]); for (const key of Object.keys(links)) if (typeof parsed[key] === "string") links[key] = parsed[key].trim(); } catch (e) { console.warn(`links.json: ${e.message}; using the built-in links`); } let releases = []; try { const res = await fetch(`https://api.github.com/repos/${REPO}/releases?per_page=100`, { // An optional token (a Railway variable, or `gh auth token` locally) lifts the rate limit. headers: { Accept: "application/vnd.github+json", ...(process.env.GITHUB_TOKEN ? { Authorization: `Bearer ${process.env.GITHUB_TOKEN}` } : {}) }, signal: AbortSignal.timeout(15000), }); if (res.ok) releases = (await res.json()).filter((r) => !r.draft); else console.warn(`releases: ${res.status} from GitHub; the page will load them itself`); } catch (e) { console.warn(`releases: ${e.message}; the page will load them itself`); } let contributors = []; try { const res = await fetch(`https://api.github.com/repos/${REPO}/contributors?per_page=100`, { headers: { Accept: "application/vnd.github+json", ...(process.env.GITHUB_TOKEN ? { Authorization: `Bearer ${process.env.GITHUB_TOKEN}` } : {}) }, signal: AbortSignal.timeout(15000), }); if (res.ok) contributors = contributorsFrom(await res.json()); else console.warn(`contributors: ${res.status} from GitHub; the page will load them itself`); } catch (e) { console.warn(`contributors: ${e.message}; the page will load them itself`); } // --- The parity table ------------------------------------------------------------------ const status = section(readme, "Project status") ?? ""; const parityHeading = (status.match(/^###\s+(Java parity:\s*[\d.]+%)/m) || [])[1]; if (!parityHeading) throw new Error("README: no '### Java parity: N%' heading under 'Project status'"); const percent = parityHeading.match(/[\d.]+%/)[0]; const parityBody = section(readme, parityHeading) ?? ""; const rows = parityBody .split("\n") .filter((l) => /^\|/.test(l) && !/^\|\s*-/.test(l)) .map((l) => l .replace(/^\|/, "") .replace(/\|\s*$/, "") .split("|") .map((c) => c.trim()), ); const [, ...body] = rows; // the first row is the header if (body.length < 5) throw new Error("README: the parity table has fewer than five rows"); const harnesses = (readme.match(/all (\d[\d,]*) (?:parity )?harnesses/) || [])[1]; if (!harnesses) throw new Error("README: no 'all N harnesses' count"); const audited = (parityBody.match(/Last audited\s+(\d{4}-\d{2}-\d{2})/) || [])[1] ?? ""; const intro = status.split(/^###/m)[0].trim(); const scoreLabel = (s) => ({ 0: "Absent", 0.25: "Started", 0.5: "Playable", 0.75: "Mostly complete", 1: "Complete" })[Number(s)] ?? s; const check = ``; const parityCells = body .map(([name, score, gaps], i) => { // "None: what matches" reads as the evidence; anything else is a gap and says so. const none = gaps.match(/^None:\s*(.*)$/); const full = Number(score) === 1; const detail = none ? inline(none[1].charAt(0).toUpperCase() + none[1].slice(1)) : `Gaps: ${inline(gaps)}`; const state = full ? `${check}Matches vanilla` : `${escape(score)} ยท ${escape(scoreLabel(score))}`; return `
${String(i + 1).padStart(2, "0")}${state}
${detail}
The release notes could not be loaded here.
Read them on GitHub`; const values = { CSS: `/assets/${cssName}`, STYLE: inlineCss, JS: `/assets/${jsName}`, PERCENT: percent, HARNESSES: harnesses, AUDITED: audited, PARITY_INTRO: markdown(intro.replace("The table below is the project's own audit", "Below is the project's own audit")), PARITY_CELLS: parityCells, PARITY_HEADLINE: parityHeadline, SUBSYSTEMS: String(body.length), NOTES: notesHtml, LAUNCHER_META: `${launcher ? `Launcher ${escape(launcher.tag_name.replace("launcher-v", ""))} for` : "For"} Linux x86-64, macOS on Apple Silicon and Windows x64.`, DISCORD: escape(links.discord || DEFAULT_LINKS.discord), SPONSOR: escape(links.sponsor || DEFAULT_LINKS.sponsor), CONTRIBUTORS: contributorsHtml(contributors), CONTRIBUTORS_HIDDEN: contributors.length ? "" : " hidden", BAKED: JSON.stringify(baked).replace(/ { if (!(key in values)) throw new Error(`${page}: unknown placeholder ${all}`); return values[key]; }); html = html.replace(/href="\/(favicon\.svg|favicon-32\.png|apple-touch-icon\.png)"/g, (all, icon) => `href="/${icon}?v=${iconVersions[icon]}"`); await writeFile(join(dist, page), html); } // The page's inline script (it marks the page as scripted before CSS applies) and its // inlined stylesheet: the Content-Security-Policy in the Caddyfile allows them by hash. const indexHtml = await readFile(join(dist, "index.html"), "utf8"); const hashes = (re) => [...new Set([...indexHtml.matchAll(re)].map((m) => `'sha256-${createHash("sha256").update(m[1]).digest("base64")}'`))].join(" "); await writeFile(join(dist, ".csp-script"), hashes(/