when-am-i / files / scripts / screenshot-calendars.mts
screenshot-calendars.mts
73912a47 2026-06-20 Pavel Safronov | 1 | // Regenerate the per-era screenshots for EVERY calendar data file at once. |
| 2 | // | |
| 3 | // npm run screenshot:calendars | |
| 4 | // | |
| 5 | // Discovers every region file in src/data/calendars/ (except the SCHEMA doc) and | |
| 6 | // shoots each one, reusing a single dev server + browser. Output mirrors the | |
| 7 | // single-file script: screenshots/calendars/<file>/. Run this after adding or | |
| 8 | // editing calendar data so the checked-in screenshots stay current. | |
| 9 | ||
| 10 | import { chromium } from 'playwright'; | |
| 11 | import { readdir } from 'node:fs/promises'; | |
| 12 | import path from 'node:path'; | |
| 13 | import { startDevServer, REPO_ROOT } from './lib/server.mts'; | |
| 14 | import { screenshotCalendar } from './lib/calendar-shots.mts'; | |
| 15 | ||
| 16 | const CALENDARS_DIR = path.join(REPO_ROOT, 'src/data/calendars'); | |
| 17 | ||
| 18 | async function run() { | |
| 19 | const files = (await readdir(CALENDARS_DIR)) | |
| 20 | .filter((f) => f.endsWith('.json') && !f.startsWith('SCHEMA')) | |
| 21 | .sort(); | |
| 22 | ||
| 23 | if (files.length === 0) { | |
| 24 | console.error('No calendar data files found in src/data/calendars/'); | |
| 25 | process.exit(1); | |
| 26 | } | |
| 27 | ||
| 28 | console.log(`\n📸 Regenerating screenshots for ${files.length} calendar(s)…`); | |
| 29 | const server = await startDevServer(); | |
| 30 | try { | |
| 31 | const browser = await chromium.launch(); | |
| 32 | const page = await browser.newPage({ viewport: { width: 1100, height: 920 }, deviceScaleFactor: 2 }); | |
| 33 | ||
| 34 | let total = 0; | |
| 35 | for (const file of files) { | |
| 36 | total += await screenshotCalendar(page, path.join(CALENDARS_DIR, file)); | |
| 37 | } | |
| 38 | ||
| 39 | await browser.close(); | |
| 40 | console.log(`\n✨ Done — ${total} shot(s) across ${files.length} calendar(s) in screenshots/calendars/\n`); | |
| 41 | } finally { | |
| 42 | server.stop(); | |
| 43 | } | |
| 44 | } | |
| 45 | ||
| 46 | run().catch((err) => { | |
| 47 | console.error(err); | |
| 48 | process.exit(1); | |
| 49 | }); |