the home site for me: also iteration 3 or 4 of my site
at main 1.6 kB view raw
1#!/usr/bin/env bun 2 3import { watch } from 'fs'; 4import { existsSync } from 'fs'; 5import { spawn } from 'child_process'; 6 7let zolaProcess: any = null; 8let isRebuilding = false; 9 10function cleanup() { 11 if (zolaProcess) { 12 zolaProcess.kill(); 13 } 14 process.exit(0); 15} 16 17process.on('SIGINT', cleanup); 18process.on('SIGTERM', cleanup); 19 20async function buildShadow() { 21 if (isRebuilding) return; 22 isRebuilding = true; 23 24 if (zolaProcess) { 25 zolaProcess.kill(); 26 zolaProcess = null; 27 } 28 29 await Bun.$`rm -rf .zola-build`.quiet(); 30 await Bun.$`mkdir -p .zola-build`.quiet(); 31 await Bun.$`cp -r content .zola-build/`.quiet(); 32 33 const optionalDirs = ['static', 'templates', 'sass', 'syntaxes']; 34 for (const dir of optionalDirs) { 35 if (existsSync(dir)) { 36 await Bun.$`cp -r ${dir} .zola-build/`.quiet(); 37 } 38 } 39 40 await Bun.$`cp config.toml .zola-build/`.quiet(); 41 await Bun.$`bun run scripts/preprocess.ts .zola-build/content`.quiet(); 42 43 zolaProcess = spawn('zola', ['serve', '--force', '--interface', '0.0.0.0', '--output-dir', '../public'], { 44 cwd: '.zola-build', 45 stdio: 'inherit' 46 }); 47 48 zolaProcess.on('error', (err: Error) => { 49 console.error('Failed to start Zola:', err); 50 }); 51 52 isRebuilding = false; 53} 54 55await buildShadow(); 56 57const watchDirs = ['content', 'templates', 'sass', 'static', 'syntaxes']; 58 59for (const dir of watchDirs) { 60 if (existsSync(dir)) { 61 watch(dir, { recursive: true }, async (event, filename) => { 62 if (filename && !filename.includes('.zola-build')) { 63 await buildShadow(); 64 } 65 }); 66 } 67} 68 69watch('config.toml', async () => { 70 await buildShadow(); 71}); 72