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.

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # gaze [![Build Status](http://img.shields.io/travis/shama/gaze.svg)](https://travis-ci.org/shama/gaze) [![Build status](https://ci.appveyor.com/api/projects/status/vtx65w9eg511tgo4)](https://ci.appveyor.com/project/shama/gaze)
  2. A globbing `fs.watch` wrapper built from the best parts of other fine watch libs.
  3. Compatible with Node.js >= 4.x, Windows, macOS, and Linux.
  4. ![gaze](http://dontkry.com/images/repos/gaze.png)
  5. [![NPM](https://nodei.co/npm/gaze.png?downloads=true)](https://nodei.co/npm/gaze/)
  6. ## Usage
  7. Install the module with: `npm install gaze` or place into your `package.json`
  8. and run `npm install`.
  9. ```javascript
  10. var gaze = require('gaze');
  11. // Watch all .js files/dirs in process.cwd()
  12. gaze('**/*.js', function(err, watcher) {
  13. // Files have all started watching
  14. // watcher === this
  15. // Get all watched files
  16. var watched = this.watched();
  17. // On file changed
  18. this.on('changed', function(filepath) {
  19. console.log(filepath + ' was changed');
  20. });
  21. // On file added
  22. this.on('added', function(filepath) {
  23. console.log(filepath + ' was added');
  24. });
  25. // On file deleted
  26. this.on('deleted', function(filepath) {
  27. console.log(filepath + ' was deleted');
  28. });
  29. // On changed/added/deleted
  30. this.on('all', function(event, filepath) {
  31. console.log(filepath + ' was ' + event);
  32. });
  33. // Get watched files with relative paths
  34. var files = this.relative();
  35. });
  36. // Also accepts an array of patterns
  37. gaze(['stylesheets/*.css', 'images/**/*.png'], function() {
  38. // Add more patterns later to be watched
  39. this.add(['js/*.js']);
  40. });
  41. ```
  42. ### Alternate Interface
  43. ```javascript
  44. var Gaze = require('gaze').Gaze;
  45. var gaze = new Gaze('**/*');
  46. // Files have all started watching
  47. gaze.on('ready', function(watcher) { });
  48. // A file has been added/changed/deleted has occurred
  49. gaze.on('all', function(event, filepath) { });
  50. ```
  51. ### Errors
  52. ```javascript
  53. gaze('**/*', function(error, watcher) {
  54. if (error) {
  55. // Handle error if it occurred while starting up
  56. }
  57. });
  58. // Or with the alternative interface
  59. var gaze = new Gaze();
  60. gaze.on('error', function(error) {
  61. // Handle error here
  62. });
  63. gaze.add('**/*');
  64. ```
  65. ### Minimatch / Glob
  66. See [isaacs's `minimatch`](https://github.com/isaacs/minimatch) for more
  67. information on glob patterns.
  68. ## Documentation
  69. ### gaze([patterns, options, callback])
  70. * `patterns` {`String`|`Array`} File patterns to be matched
  71. * `options` {`Object`}
  72. * `callback` {`Function`}
  73. * `err` {`Error` | `null`}
  74. * `watcher` {`Object`} Instance of the `Gaze` watcher
  75. ### Class: `gaze.Gaze`
  76. Create a `Gaze` object by instancing the `gaze.Gaze` class.
  77. ```javascript
  78. var Gaze = require('gaze').Gaze;
  79. var gaze = new Gaze(pattern, options, callback);
  80. ```
  81. #### Properties
  82. * `options` The options object passed in.
  83. * `interval` {integer} Interval to pass to `fs.watchFile`
  84. * `debounceDelay` {integer} Delay for events called in succession for the same
  85. file/event in milliseconds
  86. * `mode` {string} Force the watch mode. Either `'auto'` (default), `'watch'` (force native events), or `'poll'` (force stat polling).
  87. * `cwd` {string} The current working directory to base file patterns from. Default is `process.cwd()`.
  88. #### Events
  89. * `ready(watcher)` When files have been globbed and watching has begun.
  90. * `all(event, filepath)` When an `added`, `changed`, `renamed`, or `deleted` event occurs.
  91. * `added(filepath)` When a file has been added to a watch directory.
  92. * `changed(filepath)` When a file has been changed.
  93. * `deleted(filepath)` When a file has been deleted.
  94. * `renamed(newPath, oldPath)` When a file has been renamed.
  95. * `end()` When the watcher is closed and watches have been removed.
  96. * `error(err)` When an error occurs.
  97. * `nomatch` When no files have been matched.
  98. #### Methods
  99. * `emit(event, [...])` Wrapper for `EventEmitter.emit`.
  100. `added`|`changed`|`renamed`|`deleted` events will also trigger the `all` event.
  101. * `close()` Unwatch all files and reset the watch instance.
  102. * `add(patterns, callback)` Adds file(s) `patterns` to be watched.
  103. * `remove(filepath)` Removes a file or directory from being watched. Does not
  104. recurse directories.
  105. * `watched()` Returns the currently watched files.
  106. * `relative([dir, unixify])` Returns the currently watched files with relative paths.
  107. * `dir` {string} Only return relative files for this directory.
  108. * `unixify` {boolean} Return paths with `/` instead of `\\` if on Windows.
  109. ## Similar Projects
  110. Other great watch libraries to try are:
  111. * [paulmillr's `chokidar`](https://github.com/paulmillr/chokidar)
  112. * [amasad's `sane`](https://github.com/amasad/sane)
  113. * [mikeal's `watch`](https://github.com/mikeal/watch)
  114. * [github's `pathwatcher`](https://github.com/atom/node-pathwatcher)
  115. * [bevry's `watchr`](https://github.com/bevry/watchr)
  116. ## Contributing
  117. In lieu of a formal styleguide, take care to maintain the existing coding style.
  118. Add unit tests for any new or changed functionality. Lint and test your code
  119. using [grunt](http://gruntjs.com/).
  120. ## Release History
  121. * 1.1.3 - Fix for Node 10 support (@aredridel). Officially dropping support for Node < 4.
  122. * 1.1.2 - Prevent more `ENOENT` errors from escaping (@alexgorbatchev).
  123. * 1.1.1 - Prevent `fs.watch` errors from escaping error handler (@rosen-vladimirov). Fix `_addToWatched` without `path.sep` (@wyicwx).
  124. * 1.1.0 - Update to `globule@1.0.0` with `minimatch >= 3.0.0`.
  125. * 1.0.0 - Revert back to 0.5.2. Drop support for Node.js v0.8. Fix for `maxListeners`. Update `globule` to `0.2.0`.
  126. * 0.6.4 - Catch and emit `error` from `readdir` (@oconnore). Fix for `0 maxListeners`. Use `graceful-fs` to avoid `EMFILE` errors in other places `fs` is used. Better method to determine if `pathwatcher` was built. Fix keeping process alive too much, only init `pathwatcher` if a file is being watched. Set min required to Windows Vista when building on Windows (@pvolok).
  127. * 0.6.3 - Add support for Node.js v0.11
  128. * 0.6.2 - Fix argument error with `watched()`. Fix for erroneous `added` events on folders. Ignore `msvs` build error 4244.
  129. * 0.6.1 - Fix for absolute paths.
  130. * 0.6.0 - Uses native OS events (fork of `pathwatcher`) but can fall back to stat polling. Everything is async to avoid blocking, including `relative()` and `watched()`. Better error handling. Update to `globule@0.2.0`. No longer watches `cwd` by default. Added `mode` option. Better `EMFILE` message. Avoids `ENOENT` errors with symlinks. All constructor arguments are optional.
  131. * 0.5.2 - Fix for `ENOENT` error with non-existent symlinks [BACKPORTED].
  132. * 0.5.1 - Use `setImmediate` (`process.nextTick` for Node.js v0.8) to defer `ready`/`nomatch` events (@amasad).
  133. * 0.5.0 - Process is now kept alive while watching files. Emits a `nomatch` event when no files are matching.
  134. * 0.4.3 - Track file additions in newly created folders (@brett-shwom).
  135. * 0.4.2 - Fix `.remove()` method to remove a single file in a directory (@kaelzhang). Fixing “`Cannot call method 'call' of undefined`” (@krasimir). Track new file additions within folders (@brett-shwom).
  136. * 0.4.1 - Fix `watchDir` not respecting close in race condition (@chrisirhc).
  137. * 0.4.0 - Drop support for Node.js v0.6. Use `globule` for file matching. Avoid Node.js v0.10 `path.resolve`/`join` errors. Register new files when added to non-existent folder. Multiple instances can now poll the same files (@jpommerening).
  138. * 0.3.4 - Code clean up. Fix “`path must be strings`” errors (@groner). Fix incorrect `added` events (@groner).
  139. * 0.3.3 - Fix for multiple patterns with negate.
  140. * 0.3.2 - Emit `end` before `removeAllListeners`.
  141. * 0.3.1 - Fix `added` events within subfolder patterns.
  142. * 0.3.0 - Handle safewrite events, `forceWatchMethod` option removed, bug fixes and watch optimizations (@rgaskill).
  143. * 0.2.2 - Fix issue where subsequent `add` calls dont get watched (@samcday). `removeAllListeners` on `close`.
  144. * 0.2.1 - Fix issue with invalid `added` events in current working dir.
  145. * 0.2.0 - Support and mark folders with `path.sep`. Add `forceWatchMethod` option. Support `renamed` events.
  146. * 0.1.6 - Recognize the `cwd` option properly
  147. * 0.1.5 - Catch “`too many open file`” errors
  148. * 0.1.4 - Really fix the race condition with 2 watches
  149. * 0.1.3 - Fix race condition with 2 watches
  150. * 0.1.2 - Read triggering changed event fix
  151. * 0.1.1 - Minor fixes
  152. * 0.1.0 - Initial release
  153. ## License
  154. Copyright (c) 2018 Kyle Robinson Young
  155. Licensed under the MIT license.