title: Executing a file too many times?
body: <p><strong>EDIT:</strong> <em>I still have a bug where if I use the command watchit by itself without filtering in an argument I get an endless loop of processes opening without being killed. I think this could be because of the way I set up the binary for watchit to run index.js while also inside of the the watch() I set the file name to index.js by default if none is provided</em></p>
  
  <p>So I was trying to create a program similar to nodemon where it can continuously watch your the program for changes and additions/deletions of files.</p>
  
  <p>I am using 2 libraries I installed via npm, <a href="https://www.npmjs.com/package/chokidar" rel="nofollow noreferrer">chokidar</a> for the actual watching of the program, then I used <a href="https://www.npmjs.com/package/caporal" rel="nofollow noreferrer">caporal</a> to create a CLI. I am also using <a href="https://www.npmjs.com/package/lodash.debounce" rel="nofollow noreferrer">lodash.debounce</a> as well as <a href="https://www.npmjs.com/package/chalk" rel="nofollow noreferrer">chalk</a>. Chalk is simply just to change the color of the output to make it look a little cleaner</p>
  
  <p>Inside the folder I have <em>index.js, package.json, package-lock.json, node modules folder</em>, then a <em>test.js</em> which I used to just test the program with a simple console.log();</p>
  
  <p>inside <strong>package.json</strong>
  I have the bin set as:  </p>
  
  <pre><code>"bin": {
      "watchit": "index.js"
    }
  </code></pre>
  
  <p><strong>index.js</strong></p>
  
  <pre><code>#!/usr/bin/env node
  const debounce = require('lodash.debounce');
  const chokidar = require('chokidar');
  const prog = require('caporal');
  const chalk = require('chalk');
  const fs = require('fs');
  const { spawn } = require('child_process');
  
  prog
    .version('0.0.1')
    .argument('[filename]', 'Name of a file to execute')
    .action(async ({ filename }) =&gt; {
      const name = filename || 'index.js';
  
      try {
        await fs.promises.access(name);
      } catch (err) {
        throw new Error(`Could not find the file: ${name}`);
      }
      let pid;
      const start = debounce(() =&gt; {
        if (pid) {
          console.log(chalk.red('------- Ending Process -------'));
          pid.kill();
        }
        console.log(chalk.green('------- Starting Process -------'));
        pid = spawn('node', [name], { stdio: 'inherit' });
      }, 100);
  
      chokidar
        .watch('.')
        .on('add', start)
        .on('change', start)
        .on('unlink', start);
    });
  
  prog.parse(process.argv);
  </code></pre>
  
  <p><strong>test.js</strong></p>
  
  <pre><code>console.log('how many times do i run this');
  </code></pre>
  
  <p><strong>from terminal</strong></p>
  
  <pre><code>$ watchit test.js
  ------- Starting Process -------
  how many times do i run this
  ------- Ending Process -------
  ------- Starting Process -------
  how many times do i run this
  ------- Ending Process -------
  ------- Starting Process -------
  how many times do i run this
  </code></pre>
  
  <p>I am clearly creating a new process and running the program more than I should be since there are no tracked changes when the previous output was shown. That was from a single execution of test.js without ever editing it.</p>
  
  <p>Also when I simply run the command <em>watchit</em> from the directory I have this project on my computer, I get some of the above output then a large error about an unhandled promise rejection warning: <code>"UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 27)"</code></p>
  
  <p>Can anyone share some insight on WHY it's being ran too many times. Is the debounce timer just too low or is it something bigger than that?</p>
  
  <p><em>---edit---</em> after adding <code>event</code> into the <code>start</code> function and <code>console.log(event)</code> after I <code>console.log(-----start process-----)</code> I receive </p>
  
  <pre><code>------- Starting Process -------
  .git/packed-refs
  how many times do i run this
  ------- Ending Process -------
  ------- Starting Process -------
  node_modules/winston/package.json
  how many times do i run this
  ------- Ending Process -------
  ------- Starting Process -------
  node_modules/winston/node_modules/colors/lib/system/supports-colors.js
  how many times do i run this
  </code></pre>
  
  <p><strong><em>after ignoring .git and node_modules</em></strong>
  To ignore the files/folders I changed passed in the options obj:</p>
  
  <pre><code>chokidar
        .watch('.', {
          ignored: '*.git',
          ignored: 'node_modules'
        })
        .on('add', start)
        .on('change', start)
        .on('unlink', start);
    });
  
  $ watchit test.js
  ------- Starting Process -------
  .git/logs/HEAD
  how many times do i run this
  ------- Ending Process -------
  ------- Starting Process -------
  .git/logs/refs/remotes/origin/HEAD
  how many times do i run this
  </code></pre>