|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Watch visual-quickstart/content.yaml and rebuild the comic site on every save. |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * npm run watch:comic |
| 7 | + * |
| 8 | + * Leave this running while you edit content.yaml. Each time you save the file, |
| 9 | + * the comic HTML/CSS is regenerated automatically (same as `npm run build:comic`). |
| 10 | + * Press Ctrl+C to stop. |
| 11 | + * |
| 12 | + * Zero dependencies: uses Node's built-in fs.watch. It watches the directory and |
| 13 | + * filters for content.yaml so it keeps working across atomic "save = replace file" |
| 14 | + * editors (where the file is briefly renamed/recreated). |
| 15 | + */ |
| 16 | + |
| 17 | +const { spawn } = require('child_process'); |
| 18 | +const { watch, existsSync } = require('fs'); |
| 19 | +const { join } = require('path'); |
| 20 | + |
| 21 | +const repoRoot = join(__dirname, '..', '..'); // .github/scripts -> repo root |
| 22 | +const docsDir = join(repoRoot, 'visual-quickstart'); |
| 23 | +const contentFile = join(docsDir, 'content.yaml'); |
| 24 | +const buildScript = join(__dirname, 'build-comic.js'); |
| 25 | +const WATCHED = 'content.yaml'; |
| 26 | +const DEBOUNCE_MS = 200; |
| 27 | + |
| 28 | +if (!existsSync(contentFile)) { |
| 29 | + console.error(`\u2716 Content file not found:\n ${contentFile}`); |
| 30 | + process.exit(1); |
| 31 | +} |
| 32 | + |
| 33 | +let building = false; |
| 34 | +let pending = false; |
| 35 | +let timer = null; |
| 36 | + |
| 37 | +function runBuild() { |
| 38 | + if (building) { |
| 39 | + pending = true; // a change arrived mid-build; rebuild once this one finishes |
| 40 | + return; |
| 41 | + } |
| 42 | + building = true; |
| 43 | + const child = spawn('node', [buildScript], { stdio: 'inherit' }); |
| 44 | + child.on('exit', (code) => { |
| 45 | + building = false; |
| 46 | + if (code === 0) { |
| 47 | + console.log(`\u2713 Rebuilt at ${new Date().toLocaleTimeString()} \u2014 watching for changes\u2026`); |
| 48 | + } else { |
| 49 | + console.error(`\u2716 Build failed (exit ${code}) \u2014 fix the error and save again.`); |
| 50 | + } |
| 51 | + if (pending) { |
| 52 | + pending = false; |
| 53 | + runBuild(); |
| 54 | + } |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +function scheduleBuild() { |
| 59 | + clearTimeout(timer); |
| 60 | + timer = setTimeout(runBuild, DEBOUNCE_MS); |
| 61 | +} |
| 62 | + |
| 63 | +console.log(`Watching ${contentFile}\nPress Ctrl+C to stop.`); |
| 64 | +runBuild(); // build once on startup so output is fresh |
| 65 | + |
| 66 | +watch(docsDir, (_event, filename) => { |
| 67 | + if (filename === WATCHED) scheduleBuild(); |
| 68 | +}); |
| 69 | + |
| 70 | +process.on('SIGINT', () => { |
| 71 | + console.log('\nStopped watching.'); |
| 72 | + process.exit(0); |
| 73 | +}); |
0 commit comments