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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <p align="center">
  2. <a href="http://gulpjs.com">
  3. <img height="257" width="114" src="https://raw.githubusercontent.com/gulpjs/artwork/master/gulp-2x.png">
  4. </a>
  5. </p>
  6. # glob-watcher
  7. [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![AppVeyor Build Status][appveyor-image]][appveyor-url] [![Coveralls Status][coveralls-image]][coveralls-url] [![Gitter chat][gitter-image]][gitter-url]
  8. Watch globs and execute a function upon change, with intelligent defaults for debouncing and queueing.
  9. ## Usage
  10. ```js
  11. var watch = require('glob-watcher');
  12. watch(['./*.js', '!./something.js'], function(done){
  13. // This function will be called each time a globbed file is changed
  14. // but is debounced with a 200ms delay (default) and queues subsequent calls
  15. // Make sure to signal async completion with the callback
  16. // or by returning a stream, promise, observable or child process
  17. done();
  18. // if you need access to the `path` or `stat` object, listen
  19. // for the `change` event (see below)
  20. // if you need to listen to specific events, use the returned
  21. // watcher instance (see below)
  22. });
  23. // Raw chokidar instance
  24. var watcher = watch(['./*.js', '!./something.js']);
  25. // Listen for the 'change' event to get `path`/`stat`
  26. // No async completion available because this is the raw chokidar instance
  27. watcher.on('change', function(path, stat) {
  28. // `path` is the path of the changed file
  29. // `stat` is an `fs.Stat` object (not always available)
  30. });
  31. // Listen for other events
  32. // No async completion available because this is the raw chokidar instance
  33. watcher.on('add', function(path, stat) {
  34. // `path` is the path of the changed file
  35. // `stat` is an `fs.Stat` object (not always available)
  36. });
  37. ```
  38. ## API
  39. ### `watch(globs[, options][, fn])`
  40. Takes a path string, an array of path strings, a [glob][micromatch] string or an array of [glob][micromatch] strings as `globs` to watch on the filesystem. Also optionally takes `options` to configure the watcher and a `fn` to execute when a file changes.
  41. __Note: As of 5.0.0, globs must use `/` as the separator character because `\\` is reserved for escape sequences (as per the Bash 4.3 & Micromatch specs). This means you can't use `path.join()` or `__dirname` in Windows environments. If you need to use `path.join()`, you can use [normalize-path][normalize-path] against your paths afterwards. If you need to use `__dirname`, you can set it as the `cwd` option that gets passed directly to [chokidar][chokidar]. The [micromatch docs][micromatch-backslashes] contain more information about backslashes.__
  42. Returns an instance of [chokidar][chokidar].
  43. #### `fn([callback])`
  44. If the `fn` is passed, it will be called when the watcher emits a `change`, `add` or `unlink` event. It is automatically debounced with a default delay of 200 milliseconds and subsequent calls will be queued and called upon completion. These defaults can be changed using the `options`.
  45. The `fn` is passed a single argument, `callback`, which is a function that must be called when work in the `fn` is complete. Instead of calling the `callback` function, [async completion][async-completion] can be signalled by:
  46. * Returning a `Stream` or `EventEmitter`
  47. * Returning a `Child Process`
  48. * Returning a `Promise`
  49. * Returning an `Observable`
  50. Once async completion is signalled, if another run is queued, it will be executed.
  51. #### `options`
  52. ##### `options.ignoreInitial`
  53. If set to `false` the `fn` is called during [chokidar][chokidar] instantiation as it discovers the file paths. Useful if it is desirable to trigger the `fn` during startup.
  54. __Passed through to [chokidar][chokidar], but defaulted to `true` instead of `false`.__
  55. Type: `Boolean`
  56. Default: `true`
  57. ##### `options.delay`
  58. The delay to wait before triggering the `fn`. Useful for waiting on many changes before doing the work on changed files, e.g. find-and-replace on many files.
  59. Type: `Number`
  60. Default: `200` (milliseconds)
  61. ##### `options.queue`
  62. Whether or not a file change should queue the `fn` execution if the `fn` is already running. Useful for a long running `fn`.
  63. Type: `Boolean`
  64. Default: `true`
  65. ##### `options.events`
  66. An event name or array of event names to listen for. Useful if you only need to watch specific events.
  67. Type: `String | Array<String>`
  68. Default: `[ 'add', 'change', 'unlink' ]`
  69. ##### other
  70. Options are passed directly to [chokidar][chokidar].
  71. ## License
  72. MIT
  73. [micromatch]: https://github.com/micromatch/micromatch
  74. [normalize-path]: https://www.npmjs.com/package/normalize-path
  75. [micromatch-backslashes]: https://github.com/micromatch/micromatch#backslashes
  76. [async-completion]: https://github.com/gulpjs/async-done#completion-and-error-resolution
  77. [chokidar]: https://github.com/paulmillr/chokidar
  78. [downloads-image]: http://img.shields.io/npm/dm/glob-watcher.svg
  79. [npm-url]: https://npmjs.com/package/glob-watcher
  80. [npm-image]: http://img.shields.io/npm/v/glob-watcher.svg
  81. [travis-url]: https://travis-ci.org/gulpjs/glob-watcher
  82. [travis-image]: http://img.shields.io/travis/gulpjs/glob-watcher.svg?label=travis-ci
  83. [appveyor-url]: https://ci.appveyor.com/project/gulpjs/glob-watcher
  84. [appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/glob-watcher.svg?label=appveyor
  85. [coveralls-url]: https://coveralls.io/r/gulpjs/glob-watcher
  86. [coveralls-image]: http://img.shields.io/coveralls/gulpjs/glob-watcher/master.svg
  87. [gitter-url]: https://gitter.im/gulpjs/gulp
  88. [gitter-image]: https://badges.gitter.im/gulpjs/gulp.png