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 |
|
7 | # glob-watcher
|
8 |
|
9 | [![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]
|
10 |
|
11 | Watch globs and execute a function upon change, with intelligent defaults for debouncing and queueing.
|
12 |
|
13 | ## Example
|
14 |
|
15 | ```js
|
16 | var watch = require('glob-watcher');
|
17 |
|
18 | watch(['./*.js', '!./something.js'], function(done){
|
19 | // This function will be called each time a globbed file is changed
|
20 | // but is debounced with a 200ms delay (default) and queues subsequent calls
|
21 |
|
22 | // Make sure to signal async completion with the callback
|
23 | // or by returning a stream, promise, observable or child process
|
24 | done();
|
25 |
|
26 | // if you need access to the `path` or `stat` object, listen
|
27 | // for the `change` event (see below)
|
28 |
|
29 | // if you need to listen to specific events, use the returned
|
30 | // watcher instance (see below)
|
31 | });
|
32 |
|
33 | // Raw chokidar instance
|
34 | var watcher = watch(['./*.js', '!./something.js']);
|
35 |
|
36 | // Listen for the 'change' event to get `path`/`stat`
|
37 | // No async completion available because this is the raw chokidar instance
|
38 | watcher.on('change', function(path, stat) {
|
39 | // `path` is the path of the changed file
|
40 | // `stat` is an `fs.Stat` object (not always available)
|
41 | });
|
42 |
|
43 | // Listen for other events
|
44 | // No async completion available because this is the raw chokidar instance
|
45 | watcher.on('add', function(path, stat) {
|
46 | // `path` is the path of the changed file
|
47 | // `stat` is an `fs.Stat` object (not always available)
|
48 | });
|
49 | ```
|
50 |
|
51 | ## API
|
52 |
|
53 | ### `watch(globs[, options][, fn])`
|
54 |
|
55 | Takes a path string, an array of path strings, a [glob][node-glob] string or an array of [glob][node-glob] 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.
|
56 |
|
57 | Returns an instance of [chokidar][chokidar].
|
58 |
|
59 | #### `fn([callback])`
|
60 |
|
61 | 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`.
|
62 |
|
63 | 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:
|
64 | * Returning a `Stream` or `EventEmitter`
|
65 | * Returning a `Child Process`
|
66 | * Returning a `Promise`
|
67 | * Returning an `Observable`
|
68 |
|
69 | Once async completion is signalled, if another run is queued, it will be executed.
|
70 |
|
71 | #### `options`
|
72 |
|
73 | ##### `options.ignoreInitial`
|
74 |
|
75 | 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.
|
76 |
|
77 | __Passed through to [chokidar][chokidar], but defaulted to `true` instead of `false`.__
|
78 |
|
79 | Type: `Boolean`
|
80 |
|
81 | Default: `true`
|
82 |
|
83 | ##### `options.delay`
|
84 |
|
85 | 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.
|
86 |
|
87 | Type: `Number`
|
88 |
|
89 | Default: `200` (milliseconds)
|
90 |
|
91 | ##### `options.queue`
|
92 |
|
93 | Whether or not a file change should queue the `fn` execution if the `fn` is already running. Useful for a long running `fn`.
|
94 |
|
95 | Type: `Boolean`
|
96 |
|
97 | Default: `true`
|
98 |
|
99 | ##### other
|
100 |
|
101 | Options are passed directly to [lodash.debounce][lodash-debounce] and [chokidar][chokidar], so all their options are supported. Any debounce-related options are documented in [lodash.debounce][lodash-debounce]. Any chokidar-related options are documented in [chokidar][chokidar].
|
102 |
|
103 | ## License
|
104 |
|
105 | MIT
|
106 |
|
107 | [downloads-image]: http://img.shields.io/npm/dm/glob-watcher.svg
|
108 | [npm-url]: https://npmjs.com/package/glob-watcher
|
109 | [npm-image]: http://img.shields.io/npm/v/glob-watcher.svg
|
110 |
|
111 | [travis-url]: https://travis-ci.org/gulpjs/glob-watcher
|
112 | [travis-image]: http://img.shields.io/travis/gulpjs/glob-watcher.svg?label=travis-ci
|
113 |
|
114 | [appveyor-url]: https://ci.appveyor.com/project/gulpjs/glob-watcher
|
115 | [appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/glob-watcher.svg?label=appveyor
|
116 |
|
117 | [coveralls-url]: https://coveralls.io/r/gulpjs/glob-watcher
|
118 | [coveralls-image]: http://img.shields.io/coveralls/gulpjs/glob-watcher/master.svg
|
119 |
|
120 | [gitter-url]: https://gitter.im/gulpjs/gulp
|
121 | [gitter-image]: https://badges.gitter.im/gulpjs/gulp.png
|
122 |
|
123 | [glob]: https://github.com/isaacs/node-glob
|
124 | [async-completion]: https://github.com/gulpjs/async-done#completion-and-error-resolution
|
125 | [chokidar]: https://github.com/paulmillr/chokidar
|
126 | [lodash-debounce]: https://lodash.com/docs#debounce
|