|
| 1 | +import { mkdir } from "node:fs/promises"; |
| 2 | +import { dirname, resolve } from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { chromium } from "playwright"; |
| 5 | + |
| 6 | +const scriptDirectory = dirname(fileURLToPath(import.meta.url)); |
| 7 | +const projectRoot = resolve(scriptDirectory, ".."); |
| 8 | +const port = 1430; |
| 9 | +const url = `http://127.0.0.1:${port}/?screenshot=1`; |
| 10 | +const output = resolve(projectRoot, "docs/assets/screenshot.png"); |
| 11 | +const playwrightCli = resolve(projectRoot, "node_modules/playwright/cli.js"); |
| 12 | +const viteCli = resolve(projectRoot, "node_modules/vite/bin/vite.js"); |
| 13 | + |
| 14 | +let server: Bun.Subprocess | undefined; |
| 15 | +let browser: Awaited<ReturnType<typeof chromium.launch>> | undefined; |
| 16 | +let stopping: Promise<void> | undefined; |
| 17 | + |
| 18 | +async function waitForServer() { |
| 19 | + const deadline = Date.now() + 30_000; |
| 20 | + while (Date.now() < deadline) { |
| 21 | + try { |
| 22 | + const response = await fetch(url); |
| 23 | + if (response.ok) { |
| 24 | + return; |
| 25 | + } |
| 26 | + } catch {} |
| 27 | + await Bun.sleep(100); |
| 28 | + } |
| 29 | + throw new Error(`Vite did not become ready at ${url}`); |
| 30 | +} |
| 31 | + |
| 32 | +async function run(command: string[]) { |
| 33 | + const process = Bun.spawn(command, { cwd: projectRoot, stdout: "inherit", stderr: "inherit" }); |
| 34 | + if ((await process.exited) !== 0) { |
| 35 | + throw new Error(`Command failed: ${command.join(" ")}`); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +async function stopServer() { |
| 40 | + if (!server || server.exitCode !== null) { |
| 41 | + return; |
| 42 | + } |
| 43 | + server.kill("SIGTERM"); |
| 44 | + await Promise.race([server.exited.then(() => {}), Bun.sleep(5_000)]); |
| 45 | + if (server.exitCode === null) { |
| 46 | + server.kill("SIGKILL"); |
| 47 | + await server.exited; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function cleanup() { |
| 52 | + stopping ??= (async () => { |
| 53 | + await browser?.close(); |
| 54 | + await stopServer(); |
| 55 | + })(); |
| 56 | + return stopping; |
| 57 | +} |
| 58 | + |
| 59 | +for (const signal of ["SIGINT", "SIGTERM"] as const) { |
| 60 | + process.once(signal, () => { |
| 61 | + void cleanup().finally(() => process.exit(128)); |
| 62 | + }); |
| 63 | +} |
| 64 | + |
| 65 | +try { |
| 66 | + await run([process.execPath, playwrightCli, "install", "chromium"]); |
| 67 | + server = Bun.spawn( |
| 68 | + [process.execPath, viteCli, "dev", "--host", "127.0.0.1", "--port", String(port), "--strictPort"], |
| 69 | + { cwd: projectRoot, stdout: "inherit", stderr: "inherit" }, |
| 70 | + ); |
| 71 | + await waitForServer(); |
| 72 | + browser = await chromium.launch(); |
| 73 | + const context = await browser.newContext({ |
| 74 | + viewport: { width: 1120, height: 656 }, |
| 75 | + deviceScaleFactor: 2, |
| 76 | + locale: "en-US", |
| 77 | + timezoneId: "UTC", |
| 78 | + }); |
| 79 | + const page = await context.newPage(); |
| 80 | + await page.goto(url, { waitUntil: "domcontentloaded" }); |
| 81 | + await page.waitForSelector('html[data-screenshot-ready="true"]'); |
| 82 | + await page.evaluate(async () => { |
| 83 | + await document.fonts.ready; |
| 84 | + }); |
| 85 | + await page.addStyleTag({ |
| 86 | + content: "*, *::before, *::after { animation: none !important; caret-color: transparent !important; transition: none !important; }", |
| 87 | + }); |
| 88 | + await mkdir(dirname(output), { recursive: true }); |
| 89 | + await page.screenshot({ path: output, animations: "disabled" }); |
| 90 | + await context.close(); |
| 91 | +} finally { |
| 92 | + await cleanup(); |
| 93 | +} |
0 commit comments