-
-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathdev-watch.js
More file actions
78 lines (67 loc) · 2.34 KB
/
dev-watch.js
File metadata and controls
78 lines (67 loc) · 2.34 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
/* eslint-disable */
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
/**
* Simple file watcher for ramps-controller development
* Watches src/ directory and runs link script on changes
*/
const srcDir = path.join(__dirname, 'src');
const linkScript = path.join(__dirname, 'link-ramp-controller.js');
console.log('👀 Watching ramps-controller src/ directory...');
console.log('📁 Press Ctrl+C to stop watching\n');
// Keep track of last build time to avoid rapid rebuilds
let lastBuildTime = 0;
const BUILD_THROTTLE_MS = 500; // Wait 500ms after last change before building
// Function to run the link script
function runLinkScript() {
return new Promise((resolve, reject) => {
console.log('🔄 Changes detected, rebuilding and linking...');
const child = spawn('node', [linkScript], {
stdio: 'inherit',
cwd: __dirname,
});
child.on('close', (code) => {
if (code !== 0) {
console.error(`❌ Failed to rebuild and link (exit code ${code})`);
reject(new Error(`Link script exited with code ${code}`));
} else {
console.log('✅ Rebuild and link complete\n');
console.log('👀 Watching for changes...\n');
resolve();
}
});
child.on('error', (error) => {
console.error('❌ Failed to spawn link script:', error.message);
reject(error);
});
});
}
// Simple file watcher using fs.watch
function watchDirectory(dir) {
fs.watch(dir, { recursive: true }, (_eventType, filename) => {
if (filename && (filename.endsWith('.ts') || filename.endsWith('.js'))) {
const now = Date.now();
if (now - lastBuildTime > BUILD_THROTTLE_MS) {
lastBuildTime = now;
// Debounce the build to avoid multiple rapid builds
setTimeout(async () => {
if (Date.now() - lastBuildTime >= BUILD_THROTTLE_MS) {
await runLinkScript();
}
}, BUILD_THROTTLE_MS);
}
}
});
}
// Watch the src directory
watchDirectory(srcDir);
// Also watch the link script itself
fs.watchFile(linkScript, () => {
console.log('🔄 Link script changed, will rebuild on next src change');
});
// Initial build and link
console.log('🏗️ Performing initial build and link...');
runLinkScript().catch(() => {
// Initial build errors are handled within runLinkScript
});