You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.0 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. [![CircleCI](https://circleci.com/gh/amasad/sane.svg?style=svg)](https://circleci.com/gh/amasad/sane)
  2. sane
  3. ----
  4. I've been driven to insanity by node filesystem watcher wrappers.
  5. Sane aims to be fast, small, and reliable file system watcher. It does that by:
  6. * By default stays away from fs polling because it's very slow and cpu intensive
  7. * Uses `fs.watch` by default and sensibly works around the various issues
  8. * Maintains a consistent API across different platforms
  9. * Where `fs.watch` is not reliable you have the choice of using the following alternatives:
  10. * [the facebook watchman library](https://facebook.github.io/watchman/)
  11. * polling
  12. ## Install
  13. ```
  14. $ npm install sane
  15. ```
  16. ## How to choose a mode
  17. Don't worry too much about choosing the correct mode upfront because sane
  18. maintains the same API across all modes and will be easy to switch.
  19. * If you're only supporting Linux and OS X, `watchman` would be the most reliable mode
  20. * If you're using node > v0.10.0 use the default mode
  21. * If you're running OS X and you're watching a lot of directories and you're running into https://github.com/joyent/node/issues/5463, use `watchman`
  22. * If you're in an environment where native file system events aren't available (like Vagrant), you should use polling
  23. * Otherwise, the default mode should work well for you
  24. ## API
  25. ### sane(dir, options)
  26. Watches a directory and all its descendant directories for changes, deletions, and additions on files and directories.
  27. ```js
  28. var watcher = sane('path/to/dir', {glob: ['**/*.js', '**/*.css']});
  29. watcher.on('ready', function () { console.log('ready') });
  30. watcher.on('change', function (filepath, root, stat) { console.log('file changed', filepath); });
  31. watcher.on('add', function (filepath, root, stat) { console.log('file added', filepath); });
  32. watcher.on('delete', function (filepath, root) { console.log('file deleted', filepath); });
  33. // close
  34. watcher.close();
  35. ```
  36. options:
  37. * `glob`: a single string glob pattern or an array of them.
  38. * `poll`: puts the watcher in polling mode. Under the hood that means `fs.watchFile`.
  39. * `watchman`: makes the watcher use [watchman](https://facebook.github.io/watchman/).
  40. * `watchmanPath`: sets a custom path for `watchman` binary.
  41. * `dot`: enables watching files/directories that start with a dot.
  42. * `ignored`: a glob, regex, function, or array of any combination.
  43. For the glob pattern documentation, see [micromatch](https://github.com/micromatch/micromatch).
  44. If you choose to use `watchman` you'll have to [install watchman yourself](https://facebook.github.io/watchman/docs/install.html)).
  45. For the ignored options, see [anymatch](https://github.com/es128/anymatch).
  46. ### sane.NodeWatcher(dir, options)
  47. The default watcher class. Uses `fs.watch` under the hood, and takes the same options as `sane(dir, options)`.
  48. ### sane.WatchmanWatcher(dir, options)
  49. The watchman watcher class. Takes the same options as `sane(dir, options)`.
  50. ### sane.PollWatcher(dir, options)
  51. The polling watcher class. Takes the same options as `sane(dir, options)` with the addition of:
  52. * interval: indicates how often the files should be polled. (passed to fs.watchFile)
  53. ### sane.{Node|Watchman|Poll}Watcher#close
  54. Stops watching.
  55. ### sane.{Node|Watchman|Poll}Watcher events
  56. Emits the following events:
  57. All events are passed the file/dir path relative to the root directory
  58. * `ready` when the program is ready to detect events in the directory
  59. * `change` when a file changes
  60. * `add` when a file or directory has been added
  61. * `delete` when a file or directory has been deleted
  62. ## CLI
  63. This module includes a simple command line interface, which you can install with `npm install sane -g`.
  64. ```
  65. Usage: sane <command> [...directory] [--glob=<filePattern>] [--poll] [--watchman] [--watchman-path=<watchmanBinaryPath>] [--dot] [--wait=<seconds>]
  66. OPTIONS:
  67. --glob=<filePattern>
  68. A single string glob pattern or an array of them.
  69. --ignored=<filePattern>
  70. A glob, regex, function, or array of any combination.
  71. --poll, -p
  72. Use polling mode.
  73. --watchman, -w
  74. Use watchman (if available).
  75. --watchman-path=<watchmanBinaryPath>
  76. Sets a custom path for watchman binary (if using this mode).
  77. --dot, -d
  78. Enables watching files/directories that start with a dot.
  79. --wait=<seconds>
  80. Duration, in seconds, that watching will be disabled
  81. after running <command>. Setting this option will
  82. throttle calls to <command> for the specified duration.
  83. ```
  84. It will watch the given `directory` and run the given <command> every time a file changes.
  85. ### CLI example usage
  86. - `sane 'echo "A command ran"'`
  87. - `sane 'echo "A command ran"' --glob='**/*.css'`
  88. - `sane 'echo "A command ran"' site/assets/css --glob='**/*.css'`
  89. - `sane 'echo "A command ran"' --glob='**/*.css' --ignored='**/ignore.css'`
  90. - `sane 'echo "A command ran"' --wait=3`
  91. - `sane 'echo "A command ran"' -p`
  92. ## License
  93. MIT
  94. ## Credits
  95. The CLI was originally based on the [watch CLI](https://github.com/mikeal/watch). Watch is licensed under the Apache License Version 2.0.