#!/usr/bin/env node // Screenshots the demo page at a few sizes into screenshots/: desktop, mobile // and desktop-dark for the default palette, then desktop and desktop-dark for // every other palette in css/palettes/ (the page must link palettes/all.css // and honour data-palette on , as demo/index.html does). // // node scripts/screenshot.mjs # demo/index.html // node scripts/screenshot.mjs demo/x.html // // Uses puppeteer-core with the Chrome already on the machine. Set CHROME_PATH // to point at a different browser, or PUPPETEER_MODULE at a puppeteer install // to use instead of the one resolved from node_modules. import { createRequire } from "node:module"; import { createServer } from "node:http"; import { readFile, mkdir } from "node:fs/promises"; import { existsSync, readdirSync } from "node:fs"; import { extname, join, resolve, normalize, sep } from "node:path"; import { pathToFileURL } from "node:url"; const require = createRequire(import.meta.url); const repoRoot = resolve(new URL("..", import.meta.url).pathname); const page = process.argv[2] ?? "demo/index.html"; const outDir = join(repoRoot, "screenshots"); // demo/index.html → "demo", demo/palettes.html → "palettes" const stem = page.replace(/\/index\.html$/, "").replace(/\.html$/, "").split("/").pop() || "page"; const chromeCandidates = [ process.env.CHROME_PATH, "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "/Applications/Chromium.app/Contents/MacOS/Chromium", "/usr/bin/google-chrome", "/usr/bin/chromium", ].filter(Boolean); const executablePath = chromeCandidates.find((p) => existsSync(p)); if (!executablePath) { console.error("No Chrome found. Set CHROME_PATH."); process.exit(1); } const puppeteerModule = process.env.PUPPETEER_MODULE ?? "puppeteer-core"; const puppeteer = (await import(pathToFileURL(require.resolve(puppeteerModule)).href)).default; const mime = { ".html": "text/html; charset=utf-8", ".css": "text/css; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".mjs": "text/javascript; charset=utf-8", ".woff2": "font/woff2", ".woff": "font/woff", ".svg": "image/svg+xml", ".png": "image/png", ".jpg": "image/jpeg", ".json": "application/json", }; // A tiny static server so fonts and @import resolve as they would in production. const server = createServer(async (req, res) => { const url = new URL(req.url, "http://localhost"); let path = normalize(decodeURIComponent(url.pathname)); if (path.endsWith(sep)) path += "index.html"; const file = join(repoRoot, path); if (!file.startsWith(repoRoot)) { res.writeHead(403).end(); return; } try { const body = await readFile(file); res.writeHead(200, { "content-type": mime[extname(file)] ?? "application/octet-stream" }); res.end(body); } catch { res.writeHead(404).end(); } }); await new Promise((done) => server.listen(0, "127.0.0.1", done)); const { port } = server.address(); const target = `http://127.0.0.1:${port}/${page}`; const shots = [ { name: "desktop", width: 1440, height: 900, scheme: "light" }, { name: "mobile", width: 390, height: 844, scheme: "light" }, { name: "desktop-dark", width: 1440, height: 900, scheme: "dark" }, ]; const DEFAULT_PALETTE = "riso"; const palettes = readdirSync(join(repoRoot, "css", "palettes")) .filter((f) => f.endsWith(".css") && f !== "all.css") .map((f) => f.replace(/\.css$/, "")) .filter((p) => p !== DEFAULT_PALETTE); for (const palette of palettes) { shots.push({ name: `${palette}-desktop`, width: 1440, height: 900, scheme: "light", palette }); shots.push({ name: `${palette}-desktop-dark`, width: 1440, height: 900, scheme: "dark", palette }); } await mkdir(outDir, { recursive: true }); const browser = await puppeteer.launch({ executablePath, headless: true }); try { for (const shot of shots) { const tab = await browser.newPage(); await tab.setViewport({ width: shot.width, height: shot.height, deviceScaleFactor: 2 }); await tab.emulateMediaFeatures([{ name: "prefers-color-scheme", value: shot.scheme }]); await tab.goto(target, { waitUntil: "networkidle0" }); if (shot.palette) { await tab.evaluate((p) => { document.documentElement.dataset.palette = p; const picker = document.getElementById("palette-picker"); if (picker) picker.value = p; }, shot.palette); } await tab.evaluate(() => document.fonts.ready); // Chrome cannot capture more than ~16k device pixels in one go, so pick // the sharpest scale factor that keeps the full page under that limit. const pageHeight = await tab.evaluate(() => document.documentElement.scrollHeight); const deviceScaleFactor = [2, 1.5, 1].find((f) => pageHeight * f <= 16000) ?? 1; if (deviceScaleFactor !== 2) { await tab.setViewport({ width: shot.width, height: shot.height, deviceScaleFactor }); await tab.evaluate(() => document.fonts.ready); } const out = join(outDir, `${stem}-${shot.name}.png`); await tab.screenshot({ path: out, fullPage: true }); console.log(out); await tab.close(); } } finally { await browser.close(); server.close(); }