-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.js
More file actions
62 lines (52 loc) · 1.48 KB
/
dev.js
File metadata and controls
62 lines (52 loc) · 1.48 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
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const WATCH_DIRS = ['css', 'js'];
const WATCH_FILES = ['index.html'];
const DEBOUNCE_MS = 300;
let buildTimer = null;
let building = false;
function rebuild() {
if (building) {
buildTimer = setTimeout(rebuild, DEBOUNCE_MS);
return;
}
building = true;
console.log(`\n[${new Date().toLocaleTimeString()}] Rebuilding...`);
const child = spawn('node', ['build.js'], { stdio: 'inherit' });
child.on('close', (code) => {
building = false;
if (code === 0) console.log('Ready.');
else console.log(`Build exited with code ${code}`);
});
}
function scheduleRebuild(label) {
console.log(`[watch] Changed: ${label}`);
clearTimeout(buildTimer);
buildTimer = setTimeout(rebuild, DEBOUNCE_MS);
}
// Watch individual files
for (const file of WATCH_FILES) {
if (fs.existsSync(file)) {
fs.watch(file, () => scheduleRebuild(file));
}
}
// Watch directories recursively
for (const dir of WATCH_DIRS) {
if (fs.existsSync(dir)) {
fs.watch(dir, { recursive: true }, (_event, filename) => {
scheduleRebuild(path.join(dir, filename || ''));
});
}
}
console.log(`Watching: ${WATCH_FILES.join(', ')}, ${WATCH_DIRS.join('/, ')}/ for changes`);
// Initial build
rebuild();
// Start http-server on docs/
const server = spawn('npx', ['http-server', 'docs', '-p', '8080', '-c-1'], {
stdio: 'inherit',
});
process.on('SIGINT', () => {
server.kill();
process.exit();
});