Implement debounce, to stop dup rebuild when file meta changes in Linux

This commit is contained in:
Alicia Sykes 2024-03-04 20:21:33 +00:00
parent a6a2ee232a
commit 1f6bb48463
1 changed files with 18 additions and 10 deletions

View File

@ -1,20 +1,28 @@
const fs = require('fs'); const fs = require('fs');
const { exec } = require('child_process'); const { exec } = require('child_process');
const path = require('path');
const configFile = './public/conf.yml'; const configFile = path.resolve(__dirname, './public/conf.yml');
let timeout = null;
console.log(`Watching for file changes on ${configFile}`); console.log(`Watching for file changes on ${configFile}`);
fs.watch(configFile, (eventType, filename) => { fs.watch(configFile, (eventType, filename) => {
if (filename && eventType === 'change') { if (filename && eventType === 'change') {
console.log(`${filename} file Changed, running build...`); console.log(`${filename} file Changed, preparing to build...`);
exec('yarn build', (error, stdout, stderr) => { // Clear the existing timeout, if there is one
if (error) { if (timeout) clearTimeout(timeout);
console.error(`exec error: ${error}`); // Set a new timeout
return; timeout = setTimeout(() => {
} console.log('Running build...');
console.log(`stdout: ${stdout}`); exec('yarn build', (error, stdout, stderr) => {
console.error(`stderr: ${stderr}`); if (error) {
}); console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
}, 1000); // Adjust the debounce time as necessary, here it's 1000 milliseconds (1 second)
} }
}); });