From 1f6bb4846356303f107a6e0acca5450e0d60086f Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Mon, 4 Mar 2024 20:21:33 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Implement=20debounce,=20to=20stop?= =?UTF-8?q?=20dup=20rebuild=20when=20file=20meta=20changes=20in=20Linux?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/watch-for-changes.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/services/watch-for-changes.js b/services/watch-for-changes.js index c7faec2b..51b2c8f9 100644 --- a/services/watch-for-changes.js +++ b/services/watch-for-changes.js @@ -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) } });