cli.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env node
  2. var argv = require('minimist')(process.argv.slice(2))
  3. var execshell = require('exec-sh')
  4. var path = require('path')
  5. var watch = require('./main.js')
  6. if(argv._.length === 0) {
  7. console.error('Usage: watch <command> [...directory] [--wait=<seconds>] [--filter=<file>] [--interval=<seconds>] [--ignoreDotFiles] [--ignoreUnreadable]')
  8. process.exit()
  9. }
  10. var watchTreeOpts = {}
  11. var command = argv._[0]
  12. var dirs = []
  13. var i
  14. var argLen = argv._.length
  15. if (argLen > 1) {
  16. for(i = 1; i< argLen; i++) {
  17. dirs.push(argv._[i])
  18. }
  19. } else {
  20. dirs.push(process.cwd())
  21. }
  22. var waitTime = Number(argv.wait || argv.w)
  23. if (argv.interval || argv.i) {
  24. watchTreeOpts.interval = Number(argv.interval || argv.i || 0.2) * 1000.0;
  25. }
  26. if(argv.ignoreDotFiles || argv.d)
  27. watchTreeOpts.ignoreDotFiles = true
  28. if(argv.ignoreUnreadable || argv.u)
  29. watchTreeOpts.ignoreUnreadableDir = true
  30. if(argv.filter || argv.f) {
  31. try {
  32. watchTreeOpts.filter = require(path.resolve(process.cwd(), argv.filter || argv.f))
  33. } catch (e) {
  34. console.error(e)
  35. process.exit(1)
  36. }
  37. }
  38. var wait = false
  39. var dirLen = dirs.length
  40. var skip = dirLen - 1
  41. for(i = 0; i < dirLen; i++) {
  42. var dir = dirs[i]
  43. console.error('> Watching', dir)
  44. watch.watchTree(dir, watchTreeOpts, function (f, curr, prev) {
  45. if(skip) {
  46. skip--
  47. return
  48. }
  49. if(wait) return
  50. execshell(command)
  51. if(waitTime > 0) {
  52. wait = true
  53. setTimeout(function () {
  54. wait = false
  55. }, waitTime * 1000)
  56. }
  57. })
  58. }