sane
 1  #!/usr/bin/env node
 2  'use strict';
 3  
 4  const sane = require('../');
 5  const argv = require('minimist')(process.argv.slice(2));
 6  const execshell = require('exec-sh');
 7  
 8  if (argv._.length === 0) {
 9    const msg =
10      'Usage: sane <command> [...directory] [--glob=<filePattern>] ' +
11      '[--ignored=<filePattern>] [--poll] [--watchman] [--watchman-path=<watchmanBinaryPath>] [--dot] ' +
12      '[--wait=<seconds>] [--only-changes] [--quiet]';
13    console.error(msg);
14    process.exit();
15  }
16  
17  const opts = {};
18  const command = argv._[0];
19  const dir = argv._[1] || process.cwd();
20  const waitTime = Number(argv.wait || argv.w);
21  const dot = argv.dot || argv.d;
22  const glob = argv.glob || argv.g;
23  const ignored = argv.ignored || argv.i;
24  const poll = argv.poll || argv.p;
25  const watchman = argv.watchman || argv.w;
26  const watchmanPath = argv['watchman-path'];
27  const onlyChanges = argv['only-changes'] | argv.o;
28  const quiet = argv.quiet | argv.q;
29  
30  if (dot) {
31    opts.dot = true;
32  }
33  if (glob) {
34    opts.glob = glob;
35  }
36  if (ignored) {
37    opts.ignored = ignored;
38  }
39  if (poll) {
40    opts.poll = true;
41  }
42  if (watchman) {
43    opts.watchman = true;
44  }
45  if (watchmanPath) {
46    opts.watchmanPath = watchmanPath;
47  }
48  
49  let wait = false;
50  const watcher = sane(dir, opts);
51  
52  watcher.on('ready', function() {
53    if (!quiet) {
54      console.log('Watching: ', dir + '/' + (opts.glob || ''));
55    }
56    if (!onlyChanges) {
57      execshell(command);
58    }
59  });
60  
61  watcher.on('change', function(filepath) {
62    if (wait) {
63      return;
64    }
65    if (!quiet) {
66      console.log('Change detected in:', filepath);
67    }
68    execshell(command);
69  
70    if (waitTime > 0) {
71      wait = true;
72      setTimeout(function() {
73        wait = false;
74      }, waitTime * 1000);
75    }
76  });