the home site for me: also iteration 3 or 4 of my site

feat: sync just changed files

dunkirk.sh 7ceabef6 ae524368

verified
Changed files
+47 -17
scripts
+47 -17
scripts/dev.ts
··· 1 1 #!/usr/bin/env bun 2 2 3 3 import { watch } from 'fs'; 4 - import { existsSync } from 'fs'; 4 + import { existsSync, mkdirSync, copyFileSync, unlinkSync, rmSync } from 'fs'; 5 5 import { spawn } from 'child_process'; 6 + import { dirname, join } from 'path'; 6 7 7 8 let zolaProcess: any = null; 8 - let isRebuilding = false; 9 9 10 10 function cleanup() { 11 11 if (zolaProcess) { 12 12 zolaProcess.kill(); 13 13 } 14 + rmSync('.zola-build', { recursive: true, force: true }); 14 15 process.exit(0); 15 16 } 16 17 17 18 process.on('SIGINT', cleanup); 18 19 process.on('SIGTERM', cleanup); 19 20 20 - async function buildShadow() { 21 - if (isRebuilding) return; 22 - isRebuilding = true; 23 - 24 - if (zolaProcess) { 25 - zolaProcess.kill(); 26 - zolaProcess = null; 21 + function ensureDir(filePath: string) { 22 + const dir = dirname(filePath); 23 + if (!existsSync(dir)) { 24 + mkdirSync(dir, { recursive: true }); 27 25 } 26 + } 28 27 28 + function copyFile(src: string, dest: string) { 29 + ensureDir(dest); 30 + copyFileSync(src, dest); 31 + } 32 + 33 + function deleteFile(dest: string) { 34 + if (existsSync(dest)) { 35 + unlinkSync(dest); 36 + } 37 + } 38 + 39 + async function initialSync() { 29 40 await Bun.$`rm -rf .zola-build`.quiet(); 30 41 await Bun.$`mkdir -p .zola-build`.quiet(); 31 42 await Bun.$`cp -r content .zola-build/`.quiet(); ··· 39 50 40 51 await Bun.$`cp config.toml .zola-build/`.quiet(); 41 52 await Bun.$`bun run scripts/preprocess.ts .zola-build/content`.quiet(); 53 + } 54 + 55 + async function handleFileChange(dir: string, filename: string) { 56 + const srcPath = join(dir, filename); 57 + const destPath = join('.zola-build', dir, filename); 58 + 59 + if (existsSync(srcPath)) { 60 + copyFile(srcPath, destPath); 61 + if (dir === 'content' && filename.endsWith('.md')) { 62 + await Bun.$`bun run scripts/preprocess.ts ${destPath}`.quiet(); 63 + } 64 + } else { 65 + deleteFile(destPath); 66 + } 67 + } 68 + 69 + async function handleConfigChange() { 70 + copyFile('config.toml', '.zola-build/config.toml'); 71 + } 72 + 73 + async function startServer() { 74 + await initialSync(); 42 75 43 76 zolaProcess = spawn('zola', ['serve', '--force', '--interface', '0.0.0.0', '--output-dir', '../public'], { 44 77 cwd: '.zola-build', ··· 48 81 zolaProcess.on('error', (err: Error) => { 49 82 console.error('Failed to start Zola:', err); 50 83 }); 51 - 52 - isRebuilding = false; 53 84 } 54 85 55 - await buildShadow(); 86 + await startServer(); 56 87 57 88 const watchDirs = ['content', 'templates', 'sass', 'static', 'syntaxes']; 58 89 59 90 for (const dir of watchDirs) { 60 91 if (existsSync(dir)) { 61 - watch(dir, { recursive: true }, async (event, filename) => { 92 + watch(dir, { recursive: true }, (event, filename) => { 62 93 if (filename && !filename.includes('.zola-build')) { 63 - await buildShadow(); 94 + handleFileChange(dir, filename); 64 95 } 65 96 }); 66 97 } 67 98 } 68 99 69 - watch('config.toml', async () => { 70 - await buildShadow(); 100 + watch('config.toml', () => { 101 + handleConfigChange(); 71 102 }); 72 -