Repository: fgnass/node-dev
Pull request #250 (2021-04-06 09:14:55)

Poll interval
Adds --interval option passed to `filewatcher`.

Have added also --debounce option.

bjornstar: Thanks for contributing, I've opened a PR (https://github.com/whitecolor/node-dev/pull/2) on your branch to address some issues and add some tests. I'd be happy to merge this PR once that is accepted.
wclr: @bjornstar 👍 

Patch:
diff --git a/README.md b/README.md
index 21b3e03..591557b 100644
--- a/README.md
+++ b/README.md
@@ -32,6 +32,7 @@ node-dev src/server.ts
 There are a couple of command-line options that can be used to control which files are watched and what happens when they change:
 
 - `--clear` - Clear the screen on restart
+- `--debounce` - Debounce change events by time in milliseconds (non-polling mode, default: 10)
 - `--dedupe` - [Dedupe dynamically](https://www.npmjs.org/package/dynamic-dedupe)
 - `--deps`:
   - `-1` - Watch the whole dependency tree
@@ -41,6 +42,7 @@ There are a couple of command-line options that can be used to control which fil
 - `--fork` - Hook into child_process.fork
 - `--graceful_ipc <msg>` - Send 'msg' as an IPC message instead of SIGTERM for restart/shutdown
 - `--ignore` - A file whose changes should not cause a restart
+- `--interval` - Polling interval in milliseconds (default: 1000)
 - `--notify=false` - Disable desktop notifications
 - `--poll` - Force polling for file changes (Caution! CPU-heavy!)
 - `--respawn` - Keep watching for changes after the script has exited
diff --git a/lib/cfg.js b/lib/cfg.js
index ce87b52..bf0e795 100644
--- a/lib/cfg.js
+++ b/lib/cfg.js
@@ -5,6 +5,7 @@ const resolveMain = require('./resolve-main');
 
 const defaultConfig = {
   clear: false,
+  debounce: 10,
   dedupe: false,
   deps: 1,
   extensions: {
@@ -15,6 +16,7 @@ const defaultConfig = {
   fork: true,
   graceful_ipc: '',
   ignore: [],
+  interval: 1000,
   notify: true,
   poll: false,
   respawn: false,
diff --git a/lib/index.js b/lib/index.js
index 3d8fb26..fd918b9 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -17,10 +17,12 @@ module.exports = function (
   nodeArgs,
   {
     clear,
+    debounce,
     dedupe,
     deps,
     graceful_ipc: gracefulIPC,
     ignore,
+    interval,
     notify: notifyEnabled,
     poll: forcePolling,
     respawn,
@@ -57,7 +59,7 @@ module.exports = function (
   // Run ./dedupe.js as preload script
   if (dedupe) process.env.NODE_DEV_PRELOAD = localPath('dedupe');
 
-  const watcher = filewatcher({ forcePolling });
+  const watcher = filewatcher({ debounce, forcePolling, interval });
   let isPaused = false;
 
   // The child_process


Test patch:
diff --git a/test/cli.js b/test/cli.js
index 0d792dd..bf119a8 100644
--- a/test/cli.js
+++ b/test/cli.js
@@ -103,3 +103,39 @@ tap.test('--clear enables clear', t => {
   t.true(clear);
   t.done();
 });
+
+tap.test('interval default', t => {
+  const {
+    opts: { interval }
+  } = cli(['node', 'bin/node-dev', 'test']);
+
+  t.is(interval, 1000);
+  t.done();
+});
+
+tap.test('--interval=2000', t => {
+  const {
+    opts: { interval }
+  } = cli(['node', 'bin/node-dev', '--interval=2000', 'test']);
+
+  t.is(interval, 2000);
+  t.done();
+});
+
+tap.test('debounce default', t => {
+  const {
+    opts: { debounce }
+  } = cli(['node', 'bin/node-dev', 'test']);
+
+  t.is(debounce, 10);
+  t.done();
+});
+
+tap.test('--debounce=2000', t => {
+  const {
+    opts: { debounce }
+  } = cli(['node', 'bin/node-dev', '--debounce=2000', 'test']);
+
+  t.is(debounce, 2000);
+  t.done();
+});