-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡ Implement debounce, to stop dup rebuild when file meta changes in L…
…inux
- Loading branch information
Showing
1 changed file
with
18 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
const fs = require('fs'); | ||
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}`); | ||
|
||
fs.watch(configFile, (eventType, filename) => { | ||
if (filename && eventType === 'change') { | ||
console.log(`${filename} file Changed, running build...`); | ||
exec('yarn build', (error, stdout, stderr) => { | ||
if (error) { | ||
console.error(`exec error: ${error}`); | ||
return; | ||
} | ||
console.log(`stdout: ${stdout}`); | ||
console.error(`stderr: ${stderr}`); | ||
}); | ||
console.log(`${filename} file Changed, preparing to build...`); | ||
// Clear the existing timeout, if there is one | ||
if (timeout) clearTimeout(timeout); | ||
// Set a new timeout | ||
timeout = setTimeout(() => { | ||
console.log('Running build...'); | ||
exec('yarn build', (error, stdout, 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) | ||
} | ||
}); |