/** * Preview renderer: re-shoots previews/*.png from preview/*.html at a fixed 1440×860 * viewport using playwright-core (same result as the old headless Edge/Chromium recipe, * but deterministic and cross-platform). * node scripts/render-previews.mjs * * Browser binary resolution order: $CHROMIUM_PATH, then a chromium-* directory under * ~/.cache/ms-playwright (playwright cache layout), then playwright-core's registry. * playwright-core is resolved from $PWD, $PLAYWRIGHT_CORE_DIR, or the DSH web profile. */ import { createRequire } from 'node:module'; import { existsSync, readdirSync } from 'node:fs'; import { join, dirname } from 'node:path'; import { homedir } from 'node:os'; import { fileURLToPath } from 'node:url'; const root = join(dirname(fileURLToPath(import.meta.url)), '..'); const themes = [ 'catppuccin-mocha', 'catppuccin-latte', 'gruvbox-dark', 'gruvbox-light', 'everforest', 'rose-pine', 'rose-pine-dawn', 'solarized-dark', 'solarized-light', 'kanagawa', 'tokyo-night', 'tokyo-storm', 'night-owl', 'nord', 'dracula', 'one-dark', ]; function resolvePlaywright() { const bases = [process.cwd(), process.env.PLAYWRIGHT_CORE_DIR, join(homedir(), '.dsh', 'profiles', 'web')].filter(Boolean); for (const base of bases) { try { return createRequire(join(base, 'noop.js'))('playwright-core'); } catch { /* keep looking */ } } throw new Error('playwright-core not found; set PLAYWRIGHT_CORE_DIR to a directory containing node_modules/playwright-core'); } function resolveExecutable() { if (process.env.CHROMIUM_PATH) return process.env.CHROMIUM_PATH; const cache = join(homedir(), '.cache', 'ms-playwright'); if (!existsSync(cache)) return undefined; const dirs = readdirSync(cache).filter((d) => d.startsWith('chromium-')).sort().reverse(); for (const d of dirs) { for (const sub of ['chrome-linux64', 'chrome-linux']) { const bin = join(cache, d, sub, 'chrome'); if (existsSync(bin)) return bin; } } return undefined; } const pw = resolvePlaywright(); const browser = await pw.chromium.launch({ executablePath: resolveExecutable(), args: ['--no-sandbox'] }); const page = await browser.newPage({ viewport: { width: 1440, height: 860 } }); for (const t of themes) { await page.goto(`file://${join(root, 'preview', `${t}.html`)}`, { waitUntil: 'load' }); await page.screenshot({ path: join(root, 'previews', `${t}.png`) }); console.log('ok', t); } await page.goto(`file://${join(root, 'preview', 'gallery.html')}`, { waitUntil: 'load' }); await page.screenshot({ path: join(root, 'previews', '_gallery.png'), fullPage: true }); console.log('ok gallery'); await browser.close(); console.log('ALL RENDERED');