-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwatch.mjs
More file actions
101 lines (90 loc) · 2.21 KB
/
watch.mjs
File metadata and controls
101 lines (90 loc) · 2.21 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import chokidar from 'chokidar';
import { spawn } from 'child_process';
// Directories and files to watch
const watchPatterns = [
'multisynq-client.js',
'client',
'esbuild.mjs',
'esbuild-plugin-inline-worker.mjs'
];
console.log('Starting watch mode...');
console.log('Watching files:', watchPatterns);
// Debounce function to avoid multiple builds
let buildTimeout;
let isBuilding = false;
let initialBuildDone = false;
function debouncedBuild() {
if (buildTimeout) {
clearTimeout(buildTimeout);
}
buildTimeout = setTimeout(() => {
if (!isBuilding) {
if (initialBuildDone) {
console.log('\n🔄 File change detected, rebuilding...');
}
runBuild();
}
}, 500); // 500ms debounce
}
function runBuild() {
if (isBuilding) {
console.log('⏳ Build already in progress, skipping...');
return;
}
isBuilding = true;
console.log('Running build...');
const buildProcess = spawn('node', ['esbuild.mjs'], {
stdio: 'inherit',
shell: false
});
buildProcess.on('close', (code) => {
isBuilding = false;
if (code === 0) {
console.log('✅ Build completed successfully');
initialBuildDone = true;
} else {
console.log('❌ Build failed with code:', code);
}
});
}
// Initialize watcher
const watcher = chokidar.watch(watchPatterns, {
ignored: [
/(^|[\/\\])\../, // ignore dotfiles
'**/node_modules/**',
'**/dist/**',
'**/bundled/**'
],
persistent: true,
awaitWriteFinish: {
stabilityThreshold: 100,
pollInterval: 100
}
});
// Watch events
watcher
.on('ready', () => {
console.log('👀 Watcher ready.');
})
.on('change', (path) => {
console.log(`📝 File changed: ${path}`);
debouncedBuild();
})
.on('add', (path) => {
if (initialBuildDone) console.log(`➕ File added: ${path}`);
debouncedBuild();
})
.on('unlink', (path) => {
console.log(`🗑️ File removed: ${path}`);
debouncedBuild();
})
.on('error', (error) => {
console.error('Watcher error:', error);
});
// Handle process termination
process.on('SIGINT', () => {
console.log('\n🛑 Stopping watch mode...');
watcher.close();
process.exit(0);
});
console.log('Press Ctrl+C to stop watching');