|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Retakes docs/images/readme/og-card.png, the README screenshot and the |
| 4 | + * site's social card: the top 1200x630 of the built home page, light mode, |
| 5 | + * no scrollbar, through headless Chrome (or Chromium, or Edge). No dependencies. |
| 6 | + * |
| 7 | + * npm run build && node site/og-card.mjs [--out <file.png>] |
| 8 | + * |
| 9 | + * Set CHROME_PATH to pick the browser. .github/workflows/og-card.yml runs |
| 10 | + * this after each push to main that can change the home page, and commits |
| 11 | + * the result when it differs. |
| 12 | + */ |
| 13 | +import { spawn, spawnSync } from "node:child_process"; |
| 14 | +import fs from "node:fs"; |
| 15 | +import os from "node:os"; |
| 16 | +import path from "node:path"; |
| 17 | +import { fileURLToPath } from "node:url"; |
| 18 | +import { cliArgs } from "./lib/cli.mjs"; |
| 19 | +import { chromeArgs, chromeCandidates, OG_CARD_HEIGHT, OG_CARD_WIDTH, pngSize } from "./lib/og-card.mjs"; |
| 20 | +import { OG_CARD_NAME } from "./lib/shared.mjs"; |
| 21 | +import { createSiteServer } from "./serve.mjs"; |
| 22 | + |
| 23 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 24 | +const ROOT = path.join(__dirname, ".."); |
| 25 | +const DIST = path.join(__dirname, "dist"); |
| 26 | +const DEFAULT_OUT = path.join(ROOT, "docs", "images", "readme", OG_CARD_NAME); |
| 27 | + |
| 28 | +/** The first candidate that runs `--version`, or null. */ |
| 29 | +function findChrome() { |
| 30 | + for (const bin of chromeCandidates(process.platform, process.env)) { |
| 31 | + if (path.isAbsolute(bin) && !fs.existsSync(bin)) continue; |
| 32 | + // Chrome on Windows prints nothing for --version; existing is enough there. |
| 33 | + if (process.platform === "win32") return bin; |
| 34 | + const r = spawnSync(bin, ["--version"], { encoding: "utf8", timeout: 20000 }); |
| 35 | + if (r.status === 0) return bin; |
| 36 | + } |
| 37 | + return null; |
| 38 | +} |
| 39 | + |
| 40 | +/** Runs `bin` to completion (or kills it after `timeoutMs`); resolves `{ status, stderr }`. */ |
| 41 | +function run(bin, args, timeoutMs) { |
| 42 | + return new Promise((resolve) => { |
| 43 | + const child = spawn(bin, args, { stdio: ["ignore", "ignore", "pipe"] }); |
| 44 | + let stderr = ""; |
| 45 | + child.stderr.on("data", (d) => (stderr += d)); |
| 46 | + const timer = setTimeout(() => child.kill(), timeoutMs); |
| 47 | + child.on("error", (err) => { |
| 48 | + clearTimeout(timer); |
| 49 | + resolve({ status: null, stderr: String(err.message) }); |
| 50 | + }); |
| 51 | + child.on("close", (status) => { |
| 52 | + clearTimeout(timer); |
| 53 | + resolve({ status, stderr }); |
| 54 | + }); |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +async function main() { |
| 59 | + const { values: flags } = cliArgs({ out: { type: "string" } }, "node site/og-card.mjs [--out <file.png>]"); |
| 60 | + const out = path.resolve(flags.out ?? DEFAULT_OUT); |
| 61 | + if (!fs.existsSync(path.join(DIST, "index.html"))) { |
| 62 | + console.error("site/dist has no index.html; run npm run build first"); |
| 63 | + process.exit(1); |
| 64 | + } |
| 65 | + const chrome = findChrome(); |
| 66 | + if (!chrome) { |
| 67 | + console.error("No Chrome, Chromium or Edge found; set CHROME_PATH"); |
| 68 | + process.exit(1); |
| 69 | + } |
| 70 | + |
| 71 | + const server = createSiteServer(DIST); |
| 72 | + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); |
| 73 | + const { port } = server.address(); |
| 74 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "og-card-")); |
| 75 | + const shot = path.join(tmp, "shot.png"); |
| 76 | + try { |
| 77 | + // Async: the server runs in this process, so a blocking spawn would starve it. |
| 78 | + const r = await run(chrome, chromeArgs({ url: `http://127.0.0.1:${port}/`, out: shot, profileDir: path.join(tmp, "profile") }), 120000); |
| 79 | + if (!fs.existsSync(shot)) { |
| 80 | + console.error(`Chrome wrote no screenshot (exit ${r.status}).\n${r.stderr}`); |
| 81 | + process.exitCode = 1; |
| 82 | + return; |
| 83 | + } |
| 84 | + const size = pngSize(fs.readFileSync(shot)); |
| 85 | + if (!size || size.width !== OG_CARD_WIDTH || size.height !== OG_CARD_HEIGHT) { |
| 86 | + console.error(`Expected a ${OG_CARD_WIDTH}x${OG_CARD_HEIGHT} PNG, got ${size ? `${size.width}x${size.height}` : "no PNG"}`); |
| 87 | + process.exitCode = 1; |
| 88 | + return; |
| 89 | + } |
| 90 | + const before = fs.existsSync(out) ? fs.readFileSync(out) : null; |
| 91 | + const after = fs.readFileSync(shot); |
| 92 | + if (before && before.equals(after)) { |
| 93 | + console.log(`${path.relative(ROOT, out)} unchanged`); |
| 94 | + } else { |
| 95 | + fs.mkdirSync(path.dirname(out), { recursive: true }); |
| 96 | + fs.writeFileSync(out, after); |
| 97 | + console.log(`Wrote ${path.relative(ROOT, out)} (${OG_CARD_WIDTH}x${OG_CARD_HEIGHT}, ${after.length} bytes)`); |
| 98 | + } |
| 99 | + } finally { |
| 100 | + server.close(); |
| 101 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +await main(); |
0 commit comments