Files
plan2code/scripts/make-banner.mjs
T

107 lines
5.1 KiB
JavaScript

#!/usr/bin/env node
// Renders the README banner, docs/banner.jpg (2048x600: 1024x300 at twice the
// pixel density). Dev-only.
//
// node scripts/make-banner.mjs [--browser <path>]
//
// The five Plannys are the console's own step poses (the <svg data-pose> in
// src/web-console/public/index.html), drawn with its tokens.css and poses.css
// in the dark theme, held still. Rerun it when the tagline, the license or a
// step pose changes. Needs playwright-core (a root devDependency) and a local
// Chrome or Edge.
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { launchBrowser } from "./browser.mjs";
const ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
const PUBLIC = path.join(ROOT, "src", "web-console", "public");
const STEPS = [
["pathfinder", "Pathfinder"],
["plan", "Plan"],
["document", "Document"],
["implement", "Implement"],
["finalize", "Finalize"],
];
const TAGLINE = "Plan it. Approve it. Let it build.";
const FOOT = "plan2code.jparkerweb.com · Apache 2.0";
// One pose's <svg> from the console page, by its data-pose.
function pose(html, name) {
const m = new RegExp(`<svg class="pose[^"]*" data-pose="${name}"[\\s\\S]*?</svg>`).exec(html);
if (!m) throw new Error(`no data-pose="${name}" in index.html`);
return m[0];
}
// A fixed sky, so two runs draw the same picture.
function stars() {
let seed = 7;
const rand = () => ((seed = (seed * 16807) % 2147483647) / 2147483647);
const out = [];
// Kept off the words and the step labels, where a dot reads as punctuation.
const onText = (x, y) => (x < 40 && y > 15 && y < 82) || (x > 43 && y > 58 && y < 76);
while (out.length < 34) {
const blue = rand() < 0.35;
const x = rand() * 100;
const y = rand() * 100;
if (onText(x, y)) continue;
out.push(`<i style="left:${x.toFixed(2)}%;top:${y.toFixed(2)}%;width:${blue ? 3 : 2 + Math.round(rand())}px;height:${blue ? 3 : 2 + Math.round(rand())}px;background:${blue ? "var(--accent)" : "var(--ink-3)"};opacity:${(0.35 + rand() * 0.5).toFixed(2)}"></i>`);
}
return out.join("");
}
const index = fs.readFileSync(path.join(PUBLIC, "index.html"), "utf8");
const css = (f) => pathToFileURL(path.join(PUBLIC, "styles", f)).href;
const page = `<!doctype html>
<html data-theme="dark"><head><meta charset="utf-8">
<link rel="stylesheet" href="${css("tokens.css")}">
<link rel="stylesheet" href="${css("poses.css")}">
<style>
*, *::before, *::after { animation: none !important; transition: none !important; }
html, body { margin: 0; background: var(--bg); }
.banner { position: relative; width: 1024px; height: 300px; overflow: hidden; background: var(--bg); color: var(--ink); font-family: var(--sans); }
.sky i { position: absolute; border-radius: 50%; }
.words { position: absolute; left: 44px; top: 62px; }
h1 { margin: 0; font: 700 74px/1 var(--serif); letter-spacing: -1px; color: var(--ink); }
h1 b { color: var(--accent); font-weight: 700; }
.tag { margin: 22px 0 0; font-size: 23px; color: var(--ink-2); }
.tag em { font-style: normal; color: var(--accent); }
.foot { margin: 22px 0 0; font-size: 12px; font-weight: 600; letter-spacing: 2.4px; text-transform: uppercase; color: var(--ink-3); }
.steps { position: absolute; left: 462px; right: 40px; top: 86px; display: flex; justify-content: space-between; }
.path { position: absolute; left: 470px; right: 52px; top: 176px; border-top: 2px dashed var(--rule-2); }
.step { width: 104px; text-align: center; position: relative; }
.step .pose { height: 92px; width: auto; margin: 0 auto; display: block; }
.step span { display: block; margin-top: 14px; font-size: 12px; font-weight: 700; letter-spacing: 1.6px; text-transform: uppercase; color: var(--ink-2); }
</style></head><body>
<div class="banner">
<div class="sky">${stars()}</div>
<div class="words">
<h1>Plan<b>2</b>Code</h1>
<p class="tag">Plan it. Approve it. <em>Let it build.</em></p>
<p class="foot">${FOOT}</p>
</div>
<div class="path"></div>
<div class="steps">${STEPS.map(([id, label]) => `<div class="step">${pose(index, id)}<span>${label}</span></div>`).join("")}</div>
</div>
</body></html>`;
if (!page.includes(TAGLINE.split(" Let")[0])) throw new Error("tagline drifted");
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-banner-"));
const file = path.join(tmp, "banner.html");
fs.writeFileSync(file, page);
const flag = process.argv.indexOf("--browser");
const browser = await launchBrowser(flag > 0 ? { executablePath: process.argv[flag + 1] } : {});
try {
const tab = await browser.newPage({ viewport: { width: 1024, height: 300 }, deviceScaleFactor: 2, colorScheme: "dark" });
await tab.goto(pathToFileURL(file).href);
await tab.evaluate(() => document.fonts.ready);
const out = path.join(ROOT, "docs", "banner.jpg");
await tab.screenshot({ path: out, type: "jpeg", quality: 90, clip: { x: 0, y: 0, width: 1024, height: 300 } });
console.log(`wrote ${path.relative(ROOT, out)} (${Math.round(fs.statSync(out).size / 1024)} KB)`);
} finally {
await browser.close();
fs.rmSync(tmp, { recursive: true, force: true });
}