-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
index.js
60 lines (47 loc) · 1.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
var chokidar = require('chokidar');
var asyncDone = require('async-done');
var normalizeArgs = require('./lib/normalize-args');
var debounce = require('./lib/debounce');
function watch(glob, options, cb) {
return normalizeArgs(glob, options, cb, watchProc);
}
function watchProc(globs, options, cb) {
var watcher = chokidar.watch(globs, options);
registerWatchEvent(watcher, options, cb);
return watcher;
}
function registerWatchEvent(watcher, opts, cb) {
if (typeof cb !== 'function') {
return;
}
var queued = false;
var running = false;
function runComplete(err) {
running = false;
if (err && watcher.listenerCount('error') > 0) {
watcher.emit('error', err);
}
// If we have a run queued, start onChange again
if (queued) {
queued = false;
onChange();
}
}
function onChange() {
if (running) {
if (opts.queue) {
queued = true;
}
return;
}
running = true;
asyncDone(cb, runComplete);
}
var debounced = debounce(onChange, opts.delay);
opts.events.forEach(watchEvent);
function watchEvent(eventName) {
watcher.on(eventName, debounced);
}
}
module.exports = watch;