-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.js
More file actions
77 lines (62 loc) · 1.75 KB
/
Copy pathdev.js
File metadata and controls
77 lines (62 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env node
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const WATCH_DIRS = ['./content', './templates', './data', './collections', './static', './styles'];
let building = false;
let needsRebuild = false;
function build() {
if (building) {
needsRebuild = true;
return;
}
building = true;
console.log('\n🔄 Rebuilding...');
const buildProcess = spawn('node', [path.join(__dirname, 'build.js')], { stdio: 'inherit' });
buildProcess.on('close', (code) => {
building = false;
if (code === 0) {
console.log('✅ Ready');
}
if (needsRebuild) {
needsRebuild = false;
build();
}
});
}
function watchDir(dir) {
if (!fs.existsSync(dir)) return;
let debounceTimer;
const watcher = fs.watch(dir, { recursive: true }, (eventType, filename) => {
if (filename) {
// Ignore temporary editor files and hidden files
if (filename.startsWith('.') ||
filename.endsWith('~') ||
filename.endsWith('.swp') ||
filename.includes('.tmp')) {
return;
}
// Debounce to avoid multiple rapid rebuilds
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
console.log(`📝 Changed: ${path.join(dir, filename)}`);
build();
}, 100);
}
});
console.log(`👀 Watching: ${dir}`);
return watcher;
}
// Initial build
build();
// Watch directories
WATCH_DIRS.forEach(watchDir);
// Start server
console.log('🚀 Starting server...');
const serverProcess = spawn('node', [path.join(__dirname, 'serve.js')], { stdio: 'inherit' });
// Cleanup on exit
process.on('SIGINT', () => {
console.log('\n👋 Shutting down...');
serverProcess.kill();
process.exit(0);
});