Become a Pro / website · becomeapro.ai / scripts / covers / render.mjs
| 1 | // Renders the tutorial cover images into public/covers/ as 2x WebP (3200 x 1920), then their share images into public/og/ (og.mjs). |
| 2 | // npm run covers render every cover |
| 3 | // npm run covers -- claude render covers whose output path starts with "claude" |
| 4 | // Set COVERS_CHROME to a Chrome binary if Playwright's own browser isn't installed. |
| 5 | import fs from "node:fs/promises"; |
| 6 | import path from "node:path"; |
| 7 | import { chromium } from "@playwright/test"; |
| 8 | import sharp from "sharp"; |
| 9 | import { H, W, page } from "./base.mjs"; |
| 10 | import { writeOgImages } from "./og.mjs"; |
| 11 | import { COVERS, SCENES } from "./scenes.mjs"; |
| 12 | |
| 13 | const OUT_DIR = path.join(process.cwd(), "public", "covers"); |
| 14 | const only = process.argv[2]; |
| 15 | |
| 16 | const browser = await chromium.launch({ executablePath: process.env.COVERS_CHROME || undefined }); |
| 17 | try { |
| 18 | for (const cover of COVERS.filter((c) => !only || c.out.startsWith(only))) { |
| 19 | // A fresh page per cover, so each one's scripts run in a clean context |
| 20 | const tab = await browser.newPage({ viewport: { width: W, height: H }, deviceScaleFactor: 2 }); |
| 21 | const scene = SCENES[cover.scene](cover.variant); |
| 22 | const html = page({ seed: cover.out, ...(typeof scene === "string" ? { body: scene } : scene) }); |
| 23 | await tab.setContent(html, { waitUntil: "networkidle" }); |
| 24 | await tab.evaluate(() => document.fonts.ready); |
| 25 | const png = await tab.screenshot({ type: "png" }); |
| 26 | const file = path.join(OUT_DIR, `${cover.out}.webp`); |
| 27 | await fs.mkdir(path.dirname(file), { recursive: true }); |
| 28 | // 2x pixels and full-colour WebP (no chroma subsampling), so the dark gradients and small |
| 29 | // particles survive the second encode by next/image without blocking or smearing |
| 30 | await sharp(png).webp({ quality: 92, smartSubsample: true, effort: 6 }).toFile(file); |
| 31 | await tab.close(); |
| 32 | console.log(`covers: wrote ${path.relative(process.cwd(), file)}`); |
| 33 | } |
| 34 | await writeOgImages(); |
| 35 | } catch (e) { |
| 36 | console.error("covers: rendering failed:", e); |
| 37 | process.exitCode = 1; |
| 38 | } finally { |
| 39 | await browser.close(); |
| 40 | } |