cli.js 1.6 KB

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