// Assembles the game page from src/: src/index.html is the shell, and every line that is exactly // `` is replaced by the files under src/DIR, concatenated in path order. // The result is the same single, self-contained index.html the game has always shipped: one request, // no module loader, no runtime cost. See docs/ARCHITECTURE.md ("Source layout") and docs/adr/. // // node scripts/lib/page.js [-o FILE] assemble this checkout (stdout by default) // node scripts/lib/page.js --ref origin/main -o FILE assemble the page as it is at a git ref // // --ref uses that commit's own assembler, so it keeps working when this file changes; refs from before // the split (when the page was site/index.html) are read as they are. const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); const { execFileSync } = require('node:child_process'); const ROOT = path.join(__dirname, '..', '..'); const INCLUDE = /^$/; // Every file under dir, as paths relative to dir with '/' separators, in a stable order // (plain code-unit comparison, so the order never depends on the machine's locale). function listFiles(dir, rel = '') { const out = []; for (const e of fs.readdirSync(path.join(dir, rel), { withFileTypes: true })) { if (e.name.startsWith('.')) continue; const r = rel ? rel + '/' + e.name : e.name; if (e.isDirectory()) out.push(...listFiles(dir, r)); else out.push(r); } return out.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)); } // The partials that make up one include, in order: [{file, text}]. function partials(src, dir) { const base = path.join(src, dir); if (!fs.existsSync(base)) throw new Error(`@include ${dir}: no such directory under ${path.relative(ROOT, src) || '.'}`); return listFiles(base).map((r) => { const file = path.join(base, r); const text = fs.readFileSync(file, 'utf8'); if (text.includes('\r')) throw new Error(`${path.relative(ROOT, file)}: use LF line endings`); if (text && !text.endsWith('\n')) throw new Error(`${path.relative(ROOT, file)}: must end with a newline`); if (/^<\/(script|style)/im.test(text)) throw new Error(`${path.relative(ROOT, file)}: must not close its