50 lines · 2.5 KB · TypeScriptRaw
/** * Builds the site into dist/: every page from renderSite(), the static files * (fonts, icons, screenshots, badges, link previews). * * node src/site/build.ts the live build: refuses while a page holds an owner's blank * node src/site/build.ts --preview marks the blanks instead (CI, `wrangler dev`, review), and adds /listings * --eager loads every picture at once (for full-page screenshots of the pages) * * Before writing anything it runs the site's checks (checks.ts): a page that * would load from another host, or grow past its size, stops the build. */import { cpSync, mkdirSync, rmSync, writeFileSync } from "node:fs";import path from "node:path";import { fileURLToPath } from "node:url";import { blanksOf, forPreview, previewOf, refusal } from "../pages/blanks.ts";import { checkShots, checkSite } from "./checks.ts";import { APPS } from "./registry.ts";import { cottonCss } from "./style.ts";import { listingsPage, renderSite } from "./pages.ts";const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");const preview = process.argv.includes("--preview");const eager = process.argv.includes("--eager");const dist = path.join(root, "dist");const pages = Object.fromEntries(Object.entries(renderSite()).map(([file, html]) => [file, forPreview(html, preview)]));// The store words, for reading on the preview only (pages.ts listingsPage).if (preview) pages["listings.html"] = listingsPage();const found = blanksOf(pages);if (!preview && Object.keys(found).length > 0) { console.error(refusal(found)); process.exit(1);}const generated = { "/cotton.css": cottonCss(APPS) };const problems = [...checkSite(pages, path.join(root, "static"), generated), ...checkShots(APPS, path.join(root, "static"))];if (problems.length > 0) { console.error(`The site did not build:\n${problems.map((p) => ` - ${p}`).join("\n")}`); process.exit(1);}rmSync(dist, { recursive: true, force: true });mkdirSync(dist, { recursive: true });cpSync(path.join(root, "static"), dist, { recursive: true });for (const [url, text] of Object.entries(generated)) writeFileSync(path.join(dist, url.slice(1)), text);for (const [file, page] of Object.entries(pages)) { const out = preview ? previewOf(page) : page; writeFileSync(path.join(dist, file), eager ? out.replaceAll(' loading="lazy"', "") : out);}console.log(`cotton-site: ${Object.keys(pages).length} pages${preview ? " (preview, blanks marked)" : ""} → dist/`);