// Capture the visual-system styleguide at 1440 × 900 through the repository's // zero-dependency Chrome DevTools driver. Read-only import: web/test/browser.mjs // is not modified. // // node web/design/tokens/capture.mjs // // Writes PNGs into web/design/tokens/captures/ (gitignored by the root // `captures/` rule). No server is started; the styleguide is a local file. import { mkdir } from "node:fs/promises"; import { dirname, join } from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; import { launchChrome } from "../../test/browser.mjs"; const here = dirname(fileURLToPath(import.meta.url)); const outDir = join(here, "captures"); const styleguide = pathToFileURL(join(here, "styleguide.html")); const shots = [ { id: "foundations", name: "styleguide-01-foundations-1440x900.png" }, { id: "type", name: "styleguide-02-type-1440x900.png" }, { id: "space", name: "styleguide-03-space-1440x900.png" }, { id: "navigation", name: "styleguide-04-rail-1440x900.png" }, { id: "lists", name: "styleguide-05-lists-1440x900.png" }, { id: "data", name: "styleguide-06-data-1440x900.png" }, { id: "overlays", name: "styleguide-07-overlays-1440x900.png" }, { id: "conversation", name: "styleguide-08-conversation-1440x900.png" }, { id: "content", name: "styleguide-09-content-1440x900.png" }, ]; await mkdir(outDir, { recursive: true }); const browser = await launchChrome({ width: 1440, height: 900 }); try { for (const shot of shots) { const page = await browser.openPage(`${styleguide.href}#${shot.id}`, { width: 1440, height: 900, }); await page.waitForSelector(`#${shot.id}`); await page.evaluate("document.fonts.ready.then(() => true)"); if (shot.id === "foundations") { await page.evaluate("window.scrollTo(0, 0)"); } else { await page.evaluate(`(() => { const node = document.querySelector("#${shot.id}"); node.scrollIntoView({ block: "start" }); window.scrollBy(0, -16); return node.getBoundingClientRect().top; })()`); } await new Promise((resolve) => setTimeout(resolve, 180)); const path = join(outDir, shot.name); await page.screenshot(path); const metrics = await page.evaluate(`(() => { const node = document.querySelector("#${shot.id}"); return { top: Math.round(node.getBoundingClientRect().top), width: Math.round(node.getBoundingClientRect().width), pageHeight: document.documentElement.scrollHeight, }; })()`); console.log( `${shot.name}: viewport 1440x900, section top ${metrics.top}px, width ${metrics.width}px, page ${metrics.pageHeight}px`, ); await page.close(); } } finally { await browser.close(); }