|
| 1 | +#!/usr/bin/env node |
| 2 | +// Previews a dx-view document the way the dx web app frames it: view-frame.mjs and kit/ are the |
| 3 | +// same files the web app uses, so the CSP, theme tokens, kit, and bridge match. Screenshots the |
| 4 | +// view at the reply width (720px) and at phone width (360px) in light and dark themes, and prints |
| 5 | +// page errors. Needs agent-browser (see the using-agent-browser skill). |
| 6 | +// |
| 7 | +// Usage: node preview.mjs <view.html> [output-dir] [--palette daydream|deadpan] |
| 8 | +import { execFileSync } from "node:child_process"; |
| 9 | +import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 10 | +import { basename, dirname, join, resolve } from "node:path"; |
| 11 | +import { fileURLToPath } from "node:url"; |
| 12 | +import { viewDocument } from "./view-frame.mjs"; |
| 13 | + |
| 14 | +const args = process.argv.slice(2); |
| 15 | +const paletteFlag = args.indexOf("--palette"); |
| 16 | +const palette = paletteFlag === -1 ? "daydream" : args[paletteFlag + 1]; |
| 17 | +const [input, output] = args.filter( |
| 18 | + (_, index) => |
| 19 | + paletteFlag === -1 || (index !== paletteFlag && index !== paletteFlag + 1), |
| 20 | +); |
| 21 | +if (!input) { |
| 22 | + console.error( |
| 23 | + "Usage: node preview.mjs <view.html> [output-dir] [--palette daydream|deadpan]", |
| 24 | + ); |
| 25 | + process.exit(2); |
| 26 | +} |
| 27 | +const html = readFileSync(input, "utf8"); |
| 28 | +const kitDir = join(dirname(fileURLToPath(import.meta.url)), "kit"); |
| 29 | +const kit = { |
| 30 | + css: readFileSync(join(kitDir, "kit.css"), "utf8"), |
| 31 | + js: readFileSync(join(kitDir, "kit.js"), "utf8"), |
| 32 | + icons: readFileSync(join(kitDir, "icons.svg"), "utf8"), |
| 33 | +}; |
| 34 | +const outDir = resolve(output ?? `/tmp/dx-view-preview-${Date.now()}`); |
| 35 | +mkdirSync(outDir, { recursive: true }); |
| 36 | + |
| 37 | +const session = `dx-view-${process.pid}`; |
| 38 | +const browser = (...command) => |
| 39 | + execFileSync("agent-browser", ["--session", session, ...command], { |
| 40 | + encoding: "utf8", |
| 41 | + stdio: ["ignore", "pipe", "ignore"], |
| 42 | + }); |
| 43 | + |
| 44 | +const name = basename(input).replace(/\.html?$/i, ""); |
| 45 | +let failed = false; |
| 46 | +try { |
| 47 | + for (const scheme of ["light", "dark"]) { |
| 48 | + const file = join(outDir, `${name}-${scheme}.html`); |
| 49 | + writeFileSync(file, viewDocument(html, { kit, palette, scheme })); |
| 50 | + for (const width of [720, 360]) { |
| 51 | + browser("set", "viewport", String(width), "900", "2"); |
| 52 | + browser("open", `file://${file}`); |
| 53 | + browser("wait", "1400"); |
| 54 | + const height = browser( |
| 55 | + "eval", |
| 56 | + "Math.ceil(document.documentElement.getBoundingClientRect().height)", |
| 57 | + ); |
| 58 | + const shot = join(outDir, `${name}-${scheme}-${width}.png`); |
| 59 | + // Crop to the content, like the inline frame does (up to its 1200px limit). |
| 60 | + browser( |
| 61 | + "set", |
| 62 | + "viewport", |
| 63 | + String(width), |
| 64 | + String(Math.min(1200, Number(height) + 8)), |
| 65 | + "2", |
| 66 | + ); |
| 67 | + browser("screenshot", shot); |
| 68 | + const errors = JSON.parse(browser("errors", "--json")).data?.errors ?? []; |
| 69 | + console.log( |
| 70 | + `${scheme} ${width}px: ${shot} (content height ${height.trim()}px)`, |
| 71 | + ); |
| 72 | + for (const error of errors) { |
| 73 | + failed = true; |
| 74 | + console.log(` error: ${error.text} (line ${error.line})`); |
| 75 | + } |
| 76 | + browser("errors", "--clear"); |
| 77 | + } |
| 78 | + } |
| 79 | +} finally { |
| 80 | + try { |
| 81 | + browser("close"); |
| 82 | + } catch {} |
| 83 | +} |
| 84 | +if (failed) process.exitCode = 1; |
0 commit comments