forked from gulpjs/glob-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (110 loc) · 3.03 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use strict';
var chokidar = require('chokidar');
var debounce = require('just-debounce');
var asyncDone = require('async-done');
var defaults = require('object.defaults/immutable');
var isNegatedGlob = require('is-negated-glob');
var anymatch = require('anymatch');
var defaultOpts = {
delay: 200,
events: ['add', 'change', 'unlink'],
ignored: [],
ignoreInitial: true,
queue: true,
};
function listenerCount(ee, evtName) {
if (typeof ee.listenerCount === 'function') {
return ee.listenerCount(evtName);
}
return ee.listeners(evtName).length;
}
function hasErrorListener(ee) {
return listenerCount(ee, 'error') !== 0;
}
function exists(val) {
return val != null;
}
function watch(glob, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
var opt = defaults(options, defaultOpts);
if (!Array.isArray(opt.events)) {
opt.events = [opt.events];
}
if (Array.isArray(glob)) {
// We slice so we don't mutate the passed globs array
glob = glob.slice();
} else {
glob = [glob];
}
var queued = false;
var running = false;
// These use sparse arrays to keep track of the index in the
// original globs array
var positives = new Array(glob.length);
var negatives = new Array(glob.length);
// Reverse the glob here so we don't end up with a positive
// and negative glob in position 0 after a reverse
glob.reverse().forEach(sortGlobs);
function sortGlobs(globString, index) {
var result = isNegatedGlob(globString);
if (result.negated) {
negatives[index] = result.pattern;
} else {
positives[index] = result.pattern;
}
}
function shouldBeIgnored(path) {
var positiveMatch = anymatch(positives, path, true);
var negativeMatch = anymatch(negatives, path, true);
// If negativeMatch is -1, that means it was never negated
if (negativeMatch === -1) {
return false;
}
// If the negative is "less than" the positive, that means
// it came later in the glob array before we reversed them
return negativeMatch < positiveMatch;
}
var toWatch = positives.filter(exists);
// We only do add our custom `ignored` if there are some negative globs
// TODO: I'm not sure how to test this
if (negatives.some(exists)) {
opt.ignored = [].concat(opt.ignored, shouldBeIgnored);
}
var watcher = chokidar.watch(toWatch, opt);
function runComplete(err) {
running = false;
if (err && hasErrorListener(watcher)) {
watcher.emit('error', err);
}
// If we have a run queued, start onChange again
if (queued) {
queued = false;
onChange();
}
}
function onChange() {
if (running) {
if (opt.queue) {
queued = true;
}
return;
}
running = true;
asyncDone(cb, runComplete);
}
var fn;
if (typeof cb === 'function') {
fn = debounce(onChange, opt.delay);
}
function watchEvent(eventName) {
watcher.on(eventName, fn);
}
if (fn) {
opt.events.forEach(watchEvent);
}
return watcher;
}
module.exports = watch;