cli.js 1.4 KB

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