Branchwell

Graphley / app / design / app-store / render.mjs

1// Renders the App Store screenshots from real simulator captures of a fictional demo workspace.
2// node design/app-store/render.mjs <captures>/iphone <captures>/ipad <out>
3// Captures: capture.sh with a demo workspace, then with connect (see README.md). Output stays outside Git.
4import { fileURLToPath, pathToFileURL } from 'node:url';
5import { dirname, join, resolve } from 'node:path';
6import { mkdirSync } from 'node:fs';
7const { chromium } = await import(process.env.PLAYWRIGHT ?? 'playwright');
8const here = dirname(fileURLToPath(import.meta.url));
9const [phoneDir, padDir, outDir] = process.argv.slice(2).map(p => resolve(p));
10const icon = pathToFileURL(resolve(here, '../../brand/graphley-icon-1024-opaque.png')).href;
11const browser = await chromium.launch(process.env.CHROMIUM_PATH ? { executablePath: process.env.CHROMIUM_PATH } : {}); const errors = [];
12for (const [device, [width, height], dir] of [['iphone', [1320, 2868], phoneDir], ['ipad', [2752, 2064], padDir]]) {
13 const out = join(outDir, device); mkdirSync(out, { recursive: true });
14 const url = new URL(pathToFileURL(join(here, 'layouts.html')));
15 url.searchParams.set('device', device); url.searchParams.set('sources', pathToFileURL(dir).href); url.searchParams.set('icon', icon);
16 const page = await browser.newPage({ viewport: { width, height }, deviceScaleFactor: 1 });
17 page.on('pageerror', e => errors.push(String(e)));
18 await page.goto(url.href); const ids = await page.evaluate(() => window.SLIDES);
19 for (const [i, id] of ids.entries()) {
20 url.searchParams.set('slide', i + 1);
21 await page.goto(url.href);
22 await page.evaluate(() => Promise.all([...document.images].map(i => i.decode())));
23 await page.evaluate(() => document.fonts.ready);
24 const file = join(out, `${device}-${String(i + 1).padStart(2, '0')}-${id}.png`);
25 await page.screenshot({ path: file, clip: { x: 0, y: 0, width, height }, omitBackground: false });
26 console.log(file);
27 }
28 await page.close();
29}
30await browser.close();
31if (errors.length) throw Error(errors.join('\n'));