Files
pipelines/app/tool/shoot_real_app.mjs

54 lines
3.1 KiB
JavaScript

// Screenshots routes of the real app bundle at phone and desktop size.
//
// Usage (from app/):
// flutter build web -o build/web
// node tool/shoot_real_app.mjs build/web <out-dir> /chat /runs/1 /settings
//
// The bundle is served by the Runs probe fixture (tool/runs_probe_server.mjs),
// which answers the app's real protocol on loopback: no Pipelines server or
// operator data is touched. Each route is captured with Chrome DevTools device
// emulation at 390x844 (mobile) and 1280x800, as <route>-<width>.png. A
// self-contained bundle (a probe entry with its own fixture gateway, such as
// tool/pipelines_probe_entry.dart) can be served the same way with route `/`.
// SHOOT_WAIT (ms, default 4000) is the settle time per capture.
import { spawn } from 'node:child_process';
import { mkdtemp, mkdir, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { setTimeout as delay } from 'node:timers/promises';
const [build, out, ...routes] = process.argv.slice(2);
if (!build || !out || routes.length === 0) {
console.error("usage: node tool/shoot_real_app.mjs <build-dir> <out-dir> <route>...");
process.exit(64);
}
const { startFixture } = await import(new URL("./runs_probe_server.mjs", import.meta.url).href);
const fixture = await startFixture({ root: resolve(build) });
await mkdir(out, { recursive: true });
const port = 9300 + Math.floor(Math.random() * 500);
const chrome = spawn(process.env.CHROME_PATH ?? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
['--headless=new', '--disable-gpu', '--hide-scrollbars', `--remote-debugging-port=${port}`,
`--user-data-dir=${await mkdtemp(join(tmpdir(), 'shoot-'))}`, 'about:blank'], { stdio: 'ignore' });
let target;
for (let i = 0; i < 50 && !target; i++) {
try { target = (await (await fetch(`http://127.0.0.1:${port}/json`)).json()).find(t => t.type === 'page'); } catch {}
if (!target) await delay(200);
}
const ws = new WebSocket(target.webSocketDebuggerUrl);
await new Promise(r => ws.addEventListener('open', r, { once: true }));
let seq = 0; const pending = new Map();
ws.addEventListener('message', e => { const m = JSON.parse(e.data); if (m.id && pending.has(m.id)) { pending.get(m.id)(m); pending.delete(m.id); } });
const send = (method, params = {}) => new Promise(r => { const id = ++seq; pending.set(id, r); ws.send(JSON.stringify({ id, method, params })); });
try {
for (const route of routes) {
for (const [w, h, mobile] of [[390, 844, true], [1280, 800, false]]) {
await send('Emulation.setDeviceMetricsOverride', { width: w, height: h, deviceScaleFactor: 1, mobile });
await send('Page.navigate', { url: fixture.url + route });
await delay(Number(process.env.SHOOT_WAIT ?? 4000));
const shot = await send('Page.captureScreenshot', { format: 'png' });
const name = `${route.replace(/[^a-z0-9]+/gi, '_').replace(/^_|_$/g, '') || 'root'}-${w}.png`;
await writeFile(join(out, name), Buffer.from(shot.result.data, 'base64'));
console.log(join(out, name));
}
}
} finally { ws.close(); chrome.kill(); await fixture.close(); }