1 |
3a515b92
|
cagy
|
'use strict';
|
2 |
|
|
var EventEmitter = require('events').EventEmitter;
|
3 |
|
|
var fs = require('fs');
|
4 |
|
|
var sysPath = require('path');
|
5 |
|
|
var asyncEach = require('async-each');
|
6 |
|
|
var anymatch = require('anymatch');
|
7 |
|
|
var globParent = require('glob-parent');
|
8 |
|
|
var isGlob = require('is-glob');
|
9 |
|
|
var isAbsolute = require('path-is-absolute');
|
10 |
|
|
var inherits = require('inherits');
|
11 |
|
|
var braces = require('braces');
|
12 |
|
|
var normalizePath = require('normalize-path');
|
13 |
|
|
var upath = require('upath');
|
14 |
|
|
|
15 |
|
|
var NodeFsHandler = require('./lib/nodefs-handler');
|
16 |
|
|
var FsEventsHandler = require('./lib/fsevents-handler');
|
17 |
|
|
|
18 |
|
|
var arrify = function(value) {
|
19 |
|
|
if (value == null) return [];
|
20 |
|
|
return Array.isArray(value) ? value : [value];
|
21 |
|
|
};
|
22 |
|
|
|
23 |
|
|
var flatten = function(list, result) {
|
24 |
|
|
if (result == null) result = [];
|
25 |
|
|
list.forEach(function(item) {
|
26 |
|
|
if (Array.isArray(item)) {
|
27 |
|
|
flatten(item, result);
|
28 |
|
|
} else {
|
29 |
|
|
result.push(item);
|
30 |
|
|
}
|
31 |
|
|
});
|
32 |
|
|
return result;
|
33 |
|
|
};
|
34 |
|
|
|
35 |
|
|
// Little isString util for use in Array#every.
|
36 |
|
|
var isString = function(thing) {
|
37 |
|
|
return typeof thing === 'string';
|
38 |
|
|
};
|
39 |
|
|
|
40 |
|
|
// Public: Main class.
|
41 |
|
|
// Watches files & directories for changes.
|
42 |
|
|
//
|
43 |
|
|
// * _opts - object, chokidar options hash
|
44 |
|
|
//
|
45 |
|
|
// Emitted events:
|
46 |
|
|
// `add`, `addDir`, `change`, `unlink`, `unlinkDir`, `all`, `error`
|
47 |
|
|
//
|
48 |
|
|
// Examples
|
49 |
|
|
//
|
50 |
|
|
// var watcher = new FSWatcher()
|
51 |
|
|
// .add(directories)
|
52 |
|
|
// .on('add', path => console.log('File', path, 'was added'))
|
53 |
|
|
// .on('change', path => console.log('File', path, 'was changed'))
|
54 |
|
|
// .on('unlink', path => console.log('File', path, 'was removed'))
|
55 |
|
|
// .on('all', (event, path) => console.log(path, ' emitted ', event))
|
56 |
|
|
//
|
57 |
|
|
function FSWatcher(_opts) {
|
58 |
|
|
EventEmitter.call(this);
|
59 |
|
|
var opts = {};
|
60 |
|
|
// in case _opts that is passed in is a frozen object
|
61 |
|
|
if (_opts) for (var opt in _opts) opts[opt] = _opts[opt];
|
62 |
|
|
this._watched = Object.create(null);
|
63 |
|
|
this._closers = Object.create(null);
|
64 |
|
|
this._ignoredPaths = Object.create(null);
|
65 |
|
|
Object.defineProperty(this, '_globIgnored', {
|
66 |
|
|
get: function() { return Object.keys(this._ignoredPaths); }
|
67 |
|
|
});
|
68 |
|
|
this.closed = false;
|
69 |
|
|
this._throttled = Object.create(null);
|
70 |
|
|
this._symlinkPaths = Object.create(null);
|
71 |
|
|
|
72 |
|
|
function undef(key) {
|
73 |
|
|
return opts[key] === undefined;
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
// Set up default options.
|
77 |
|
|
if (undef('persistent')) opts.persistent = true;
|
78 |
|
|
if (undef('ignoreInitial')) opts.ignoreInitial = false;
|
79 |
|
|
if (undef('ignorePermissionErrors')) opts.ignorePermissionErrors = false;
|
80 |
|
|
if (undef('interval')) opts.interval = 100;
|
81 |
|
|
if (undef('binaryInterval')) opts.binaryInterval = 300;
|
82 |
|
|
if (undef('disableGlobbing')) opts.disableGlobbing = false;
|
83 |
|
|
this.enableBinaryInterval = opts.binaryInterval !== opts.interval;
|
84 |
|
|
|
85 |
|
|
// Enable fsevents on OS X when polling isn't explicitly enabled.
|
86 |
|
|
if (undef('useFsEvents')) opts.useFsEvents = !opts.usePolling;
|
87 |
|
|
|
88 |
|
|
// If we can't use fsevents, ensure the options reflect it's disabled.
|
89 |
|
|
if (!FsEventsHandler.canUse()) opts.useFsEvents = false;
|
90 |
|
|
|
91 |
|
|
// Use polling on Mac if not using fsevents.
|
92 |
|
|
// Other platforms use non-polling fs.watch.
|
93 |
|
|
if (undef('usePolling') && !opts.useFsEvents) {
|
94 |
|
|
opts.usePolling = process.platform === 'darwin';
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
// Global override (useful for end-developers that need to force polling for all
|
98 |
|
|
// instances of chokidar, regardless of usage/dependency depth)
|
99 |
|
|
var envPoll = process.env.CHOKIDAR_USEPOLLING;
|
100 |
|
|
if (envPoll !== undefined) {
|
101 |
|
|
var envLower = envPoll.toLowerCase();
|
102 |
|
|
|
103 |
|
|
if (envLower === 'false' || envLower === '0') {
|
104 |
|
|
opts.usePolling = false;
|
105 |
|
|
} else if (envLower === 'true' || envLower === '1') {
|
106 |
|
|
opts.usePolling = true;
|
107 |
|
|
} else {
|
108 |
|
|
opts.usePolling = !!envLower
|
109 |
|
|
}
|
110 |
|
|
}
|
111 |
|
|
var envInterval = process.env.CHOKIDAR_INTERVAL;
|
112 |
|
|
if (envInterval) {
|
113 |
|
|
opts.interval = parseInt(envInterval);
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
// Editor atomic write normalization enabled by default with fs.watch
|
117 |
|
|
if (undef('atomic')) opts.atomic = !opts.usePolling && !opts.useFsEvents;
|
118 |
|
|
if (opts.atomic) this._pendingUnlinks = Object.create(null);
|
119 |
|
|
|
120 |
|
|
if (undef('followSymlinks')) opts.followSymlinks = true;
|
121 |
|
|
|
122 |
|
|
if (undef('awaitWriteFinish')) opts.awaitWriteFinish = false;
|
123 |
|
|
if (opts.awaitWriteFinish === true) opts.awaitWriteFinish = {};
|
124 |
|
|
var awf = opts.awaitWriteFinish;
|
125 |
|
|
if (awf) {
|
126 |
|
|
if (!awf.stabilityThreshold) awf.stabilityThreshold = 2000;
|
127 |
|
|
if (!awf.pollInterval) awf.pollInterval = 100;
|
128 |
|
|
|
129 |
|
|
this._pendingWrites = Object.create(null);
|
130 |
|
|
}
|
131 |
|
|
if (opts.ignored) opts.ignored = arrify(opts.ignored);
|
132 |
|
|
|
133 |
|
|
this._isntIgnored = function(path, stat) {
|
134 |
|
|
return !this._isIgnored(path, stat);
|
135 |
|
|
}.bind(this);
|
136 |
|
|
|
137 |
|
|
var readyCalls = 0;
|
138 |
|
|
this._emitReady = function() {
|
139 |
|
|
if (++readyCalls >= this._readyCount) {
|
140 |
|
|
this._emitReady = Function.prototype;
|
141 |
|
|
this._readyEmitted = true;
|
142 |
|
|
// use process.nextTick to allow time for listener to be bound
|
143 |
|
|
process.nextTick(this.emit.bind(this, 'ready'));
|
144 |
|
|
}
|
145 |
|
|
}.bind(this);
|
146 |
|
|
|
147 |
|
|
this.options = opts;
|
148 |
|
|
|
149 |
|
|
// You’re frozen when your heart’s not open.
|
150 |
|
|
Object.freeze(opts);
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
inherits(FSWatcher, EventEmitter);
|
154 |
|
|
|
155 |
|
|
// Common helpers
|
156 |
|
|
// --------------
|
157 |
|
|
|
158 |
|
|
// Private method: Normalize and emit events
|
159 |
|
|
//
|
160 |
|
|
// * event - string, type of event
|
161 |
|
|
// * path - string, file or directory path
|
162 |
|
|
// * val[1..3] - arguments to be passed with event
|
163 |
|
|
//
|
164 |
|
|
// Returns the error if defined, otherwise the value of the
|
165 |
|
|
// FSWatcher instance's `closed` flag
|
166 |
|
|
FSWatcher.prototype._emit = function(event, path, val1, val2, val3) {
|
167 |
|
|
if (this.options.cwd) path = sysPath.relative(this.options.cwd, path);
|
168 |
|
|
var args = [event, path];
|
169 |
|
|
if (val3 !== undefined) args.push(val1, val2, val3);
|
170 |
|
|
else if (val2 !== undefined) args.push(val1, val2);
|
171 |
|
|
else if (val1 !== undefined) args.push(val1);
|
172 |
|
|
|
173 |
|
|
var awf = this.options.awaitWriteFinish;
|
174 |
|
|
if (awf && this._pendingWrites[path]) {
|
175 |
|
|
this._pendingWrites[path].lastChange = new Date();
|
176 |
|
|
return this;
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
if (this.options.atomic) {
|
180 |
|
|
if (event === 'unlink') {
|
181 |
|
|
this._pendingUnlinks[path] = args;
|
182 |
|
|
setTimeout(function() {
|
183 |
|
|
Object.keys(this._pendingUnlinks).forEach(function(path) {
|
184 |
|
|
this.emit.apply(this, this._pendingUnlinks[path]);
|
185 |
|
|
this.emit.apply(this, ['all'].concat(this._pendingUnlinks[path]));
|
186 |
|
|
delete this._pendingUnlinks[path];
|
187 |
|
|
}.bind(this));
|
188 |
|
|
}.bind(this), typeof this.options.atomic === "number"
|
189 |
|
|
? this.options.atomic
|
190 |
|
|
: 100);
|
191 |
|
|
return this;
|
192 |
|
|
} else if (event === 'add' && this._pendingUnlinks[path]) {
|
193 |
|
|
event = args[0] = 'change';
|
194 |
|
|
delete this._pendingUnlinks[path];
|
195 |
|
|
}
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
var emitEvent = function() {
|
199 |
|
|
this.emit.apply(this, args);
|
200 |
|
|
if (event !== 'error') this.emit.apply(this, ['all'].concat(args));
|
201 |
|
|
}.bind(this);
|
202 |
|
|
|
203 |
|
|
if (awf && (event === 'add' || event === 'change') && this._readyEmitted) {
|
204 |
|
|
var awfEmit = function(err, stats) {
|
205 |
|
|
if (err) {
|
206 |
|
|
event = args[0] = 'error';
|
207 |
|
|
args[1] = err;
|
208 |
|
|
emitEvent();
|
209 |
|
|
} else if (stats) {
|
210 |
|
|
// if stats doesn't exist the file must have been deleted
|
211 |
|
|
if (args.length > 2) {
|
212 |
|
|
args[2] = stats;
|
213 |
|
|
} else {
|
214 |
|
|
args.push(stats);
|
215 |
|
|
}
|
216 |
|
|
emitEvent();
|
217 |
|
|
}
|
218 |
|
|
};
|
219 |
|
|
|
220 |
|
|
this._awaitWriteFinish(path, awf.stabilityThreshold, event, awfEmit);
|
221 |
|
|
return this;
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
if (event === 'change') {
|
225 |
|
|
if (!this._throttle('change', path, 50)) return this;
|
226 |
|
|
}
|
227 |
|
|
|
228 |
|
|
if (
|
229 |
|
|
this.options.alwaysStat && val1 === undefined &&
|
230 |
|
|
(event === 'add' || event === 'addDir' || event === 'change')
|
231 |
|
|
) {
|
232 |
|
|
var fullPath = this.options.cwd ? sysPath.join(this.options.cwd, path) : path;
|
233 |
|
|
fs.stat(fullPath, function(error, stats) {
|
234 |
|
|
// Suppress event when fs.stat fails, to avoid sending undefined 'stat'
|
235 |
|
|
if (error || !stats) return;
|
236 |
|
|
|
237 |
|
|
args.push(stats);
|
238 |
|
|
emitEvent();
|
239 |
|
|
});
|
240 |
|
|
} else {
|
241 |
|
|
emitEvent();
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
return this;
|
245 |
|
|
};
|
246 |
|
|
|
247 |
|
|
// Private method: Common handler for errors
|
248 |
|
|
//
|
249 |
|
|
// * error - object, Error instance
|
250 |
|
|
//
|
251 |
|
|
// Returns the error if defined, otherwise the value of the
|
252 |
|
|
// FSWatcher instance's `closed` flag
|
253 |
|
|
FSWatcher.prototype._handleError = function(error) {
|
254 |
|
|
var code = error && error.code;
|
255 |
|
|
var ipe = this.options.ignorePermissionErrors;
|
256 |
|
|
if (error &&
|
257 |
|
|
code !== 'ENOENT' &&
|
258 |
|
|
code !== 'ENOTDIR' &&
|
259 |
|
|
(!ipe || (code !== 'EPERM' && code !== 'EACCES'))
|
260 |
|
|
) this.emit('error', error);
|
261 |
|
|
return error || this.closed;
|
262 |
|
|
};
|
263 |
|
|
|
264 |
|
|
// Private method: Helper utility for throttling
|
265 |
|
|
//
|
266 |
|
|
// * action - string, type of action being throttled
|
267 |
|
|
// * path - string, path being acted upon
|
268 |
|
|
// * timeout - int, duration of time to suppress duplicate actions
|
269 |
|
|
//
|
270 |
|
|
// Returns throttle tracking object or false if action should be suppressed
|
271 |
|
|
FSWatcher.prototype._throttle = function(action, path, timeout) {
|
272 |
|
|
if (!(action in this._throttled)) {
|
273 |
|
|
this._throttled[action] = Object.create(null);
|
274 |
|
|
}
|
275 |
|
|
var throttled = this._throttled[action];
|
276 |
|
|
if (path in throttled) {
|
277 |
|
|
throttled[path].count++;
|
278 |
|
|
return false;
|
279 |
|
|
}
|
280 |
|
|
function clear() {
|
281 |
|
|
var count = throttled[path] ? throttled[path].count : 0;
|
282 |
|
|
delete throttled[path];
|
283 |
|
|
clearTimeout(timeoutObject);
|
284 |
|
|
return count;
|
285 |
|
|
}
|
286 |
|
|
var timeoutObject = setTimeout(clear, timeout);
|
287 |
|
|
throttled[path] = {timeoutObject: timeoutObject, clear: clear, count: 0};
|
288 |
|
|
return throttled[path];
|
289 |
|
|
};
|
290 |
|
|
|
291 |
|
|
// Private method: Awaits write operation to finish
|
292 |
|
|
//
|
293 |
|
|
// * path - string, path being acted upon
|
294 |
|
|
// * threshold - int, time in milliseconds a file size must be fixed before
|
295 |
|
|
// acknowledging write operation is finished
|
296 |
|
|
// * awfEmit - function, to be called when ready for event to be emitted
|
297 |
|
|
// Polls a newly created file for size variations. When files size does not
|
298 |
|
|
// change for 'threshold' milliseconds calls callback.
|
299 |
|
|
FSWatcher.prototype._awaitWriteFinish = function(path, threshold, event, awfEmit) {
|
300 |
|
|
var timeoutHandler;
|
301 |
|
|
|
302 |
|
|
var fullPath = path;
|
303 |
|
|
if (this.options.cwd && !isAbsolute(path)) {
|
304 |
|
|
fullPath = sysPath.join(this.options.cwd, path);
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
var now = new Date();
|
308 |
|
|
|
309 |
|
|
var awaitWriteFinish = (function (prevStat) {
|
310 |
|
|
fs.stat(fullPath, function(err, curStat) {
|
311 |
|
|
if (err || !(path in this._pendingWrites)) {
|
312 |
|
|
if (err && err.code !== 'ENOENT') awfEmit(err);
|
313 |
|
|
return;
|
314 |
|
|
}
|
315 |
|
|
|
316 |
|
|
var now = new Date();
|
317 |
|
|
|
318 |
|
|
if (prevStat && curStat.size != prevStat.size) {
|
319 |
|
|
this._pendingWrites[path].lastChange = now;
|
320 |
|
|
}
|
321 |
|
|
|
322 |
|
|
if (now - this._pendingWrites[path].lastChange >= threshold) {
|
323 |
|
|
delete this._pendingWrites[path];
|
324 |
|
|
awfEmit(null, curStat);
|
325 |
|
|
} else {
|
326 |
|
|
timeoutHandler = setTimeout(
|
327 |
|
|
awaitWriteFinish.bind(this, curStat),
|
328 |
|
|
this.options.awaitWriteFinish.pollInterval
|
329 |
|
|
);
|
330 |
|
|
}
|
331 |
|
|
}.bind(this));
|
332 |
|
|
}.bind(this));
|
333 |
|
|
|
334 |
|
|
if (!(path in this._pendingWrites)) {
|
335 |
|
|
this._pendingWrites[path] = {
|
336 |
|
|
lastChange: now,
|
337 |
|
|
cancelWait: function() {
|
338 |
|
|
delete this._pendingWrites[path];
|
339 |
|
|
clearTimeout(timeoutHandler);
|
340 |
|
|
return event;
|
341 |
|
|
}.bind(this)
|
342 |
|
|
};
|
343 |
|
|
timeoutHandler = setTimeout(
|
344 |
|
|
awaitWriteFinish.bind(this),
|
345 |
|
|
this.options.awaitWriteFinish.pollInterval
|
346 |
|
|
);
|
347 |
|
|
}
|
348 |
|
|
};
|
349 |
|
|
|
350 |
|
|
// Private method: Determines whether user has asked to ignore this path
|
351 |
|
|
//
|
352 |
|
|
// * path - string, path to file or directory
|
353 |
|
|
// * stats - object, result of fs.stat
|
354 |
|
|
//
|
355 |
|
|
// Returns boolean
|
356 |
|
|
var dotRe = /\..*\.(sw[px])$|\~$|\.subl.*\.tmp/;
|
357 |
|
|
FSWatcher.prototype._isIgnored = function(path, stats) {
|
358 |
|
|
if (this.options.atomic && dotRe.test(path)) return true;
|
359 |
|
|
|
360 |
|
|
if (!this._userIgnored) {
|
361 |
|
|
var cwd = this.options.cwd;
|
362 |
|
|
var ignored = this.options.ignored;
|
363 |
|
|
if (cwd && ignored) {
|
364 |
|
|
ignored = ignored.map(function (path) {
|
365 |
|
|
if (typeof path !== 'string') return path;
|
366 |
|
|
return upath.normalize(isAbsolute(path) ? path : sysPath.join(cwd, path));
|
367 |
|
|
});
|
368 |
|
|
}
|
369 |
|
|
var paths = arrify(ignored)
|
370 |
|
|
.filter(function(path) {
|
371 |
|
|
return typeof path === 'string' && !isGlob(path);
|
372 |
|
|
}).map(function(path) {
|
373 |
|
|
return path + '/**';
|
374 |
|
|
});
|
375 |
|
|
this._userIgnored = anymatch(
|
376 |
|
|
this._globIgnored.concat(ignored).concat(paths)
|
377 |
|
|
);
|
378 |
|
|
}
|
379 |
|
|
|
380 |
|
|
return this._userIgnored([path, stats]);
|
381 |
|
|
};
|
382 |
|
|
|
383 |
|
|
// Private method: Provides a set of common helpers and properties relating to
|
384 |
|
|
// symlink and glob handling
|
385 |
|
|
//
|
386 |
|
|
// * path - string, file, directory, or glob pattern being watched
|
387 |
|
|
// * depth - int, at any depth > 0, this isn't a glob
|
388 |
|
|
//
|
389 |
|
|
// Returns object containing helpers for this path
|
390 |
|
|
var replacerRe = /^\.[\/\\]/;
|
391 |
|
|
FSWatcher.prototype._getWatchHelpers = function(path, depth) {
|
392 |
|
|
path = path.replace(replacerRe, '');
|
393 |
|
|
var watchPath = depth || this.options.disableGlobbing || !isGlob(path) ? path : globParent(path);
|
394 |
|
|
var fullWatchPath = sysPath.resolve(watchPath);
|
395 |
|
|
var hasGlob = watchPath !== path;
|
396 |
|
|
var globFilter = hasGlob ? anymatch(path) : false;
|
397 |
|
|
var follow = this.options.followSymlinks;
|
398 |
|
|
var globSymlink = hasGlob && follow ? null : false;
|
399 |
|
|
|
400 |
|
|
var checkGlobSymlink = function(entry) {
|
401 |
|
|
// only need to resolve once
|
402 |
|
|
// first entry should always have entry.parentDir === ''
|
403 |
|
|
if (globSymlink == null) {
|
404 |
|
|
globSymlink = entry.fullParentDir === fullWatchPath ? false : {
|
405 |
|
|
realPath: entry.fullParentDir,
|
406 |
|
|
linkPath: fullWatchPath
|
407 |
|
|
};
|
408 |
|
|
}
|
409 |
|
|
|
410 |
|
|
if (globSymlink) {
|
411 |
|
|
return entry.fullPath.replace(globSymlink.realPath, globSymlink.linkPath);
|
412 |
|
|
}
|
413 |
|
|
|
414 |
|
|
return entry.fullPath;
|
415 |
|
|
};
|
416 |
|
|
|
417 |
|
|
var entryPath = function(entry) {
|
418 |
|
|
return sysPath.join(watchPath,
|
419 |
|
|
sysPath.relative(watchPath, checkGlobSymlink(entry))
|
420 |
|
|
);
|
421 |
|
|
};
|
422 |
|
|
|
423 |
|
|
var filterPath = function(entry) {
|
424 |
|
|
if (entry.stat && entry.stat.isSymbolicLink()) return filterDir(entry);
|
425 |
|
|
var resolvedPath = entryPath(entry);
|
426 |
|
|
return (!hasGlob || globFilter(resolvedPath)) &&
|
427 |
|
|
this._isntIgnored(resolvedPath, entry.stat) &&
|
428 |
|
|
(this.options.ignorePermissionErrors ||
|
429 |
|
|
this._hasReadPermissions(entry.stat));
|
430 |
|
|
}.bind(this);
|
431 |
|
|
|
432 |
|
|
var getDirParts = function(path) {
|
433 |
|
|
if (!hasGlob) return false;
|
434 |
|
|
var parts = [];
|
435 |
|
|
var expandedPath = braces.expand(path);
|
436 |
|
|
expandedPath.forEach(function(path) {
|
437 |
|
|
parts.push(sysPath.relative(watchPath, path).split(/[\/\\]/));
|
438 |
|
|
});
|
439 |
|
|
return parts;
|
440 |
|
|
};
|
441 |
|
|
|
442 |
|
|
var dirParts = getDirParts(path);
|
443 |
|
|
if (dirParts) {
|
444 |
|
|
dirParts.forEach(function(parts) {
|
445 |
|
|
if (parts.length > 1) parts.pop();
|
446 |
|
|
});
|
447 |
|
|
}
|
448 |
|
|
var unmatchedGlob;
|
449 |
|
|
|
450 |
|
|
var filterDir = function(entry) {
|
451 |
|
|
if (hasGlob) {
|
452 |
|
|
var entryParts = getDirParts(checkGlobSymlink(entry));
|
453 |
|
|
var globstar = false;
|
454 |
|
|
unmatchedGlob = !dirParts.some(function(parts) {
|
455 |
|
|
return parts.every(function(part, i) {
|
456 |
|
|
if (part === '**') globstar = true;
|
457 |
|
|
return globstar || !entryParts[0][i] || anymatch(part, entryParts[0][i]);
|
458 |
|
|
});
|
459 |
|
|
});
|
460 |
|
|
}
|
461 |
|
|
return !unmatchedGlob && this._isntIgnored(entryPath(entry), entry.stat);
|
462 |
|
|
}.bind(this);
|
463 |
|
|
|
464 |
|
|
return {
|
465 |
|
|
followSymlinks: follow,
|
466 |
|
|
statMethod: follow ? 'stat' : 'lstat',
|
467 |
|
|
path: path,
|
468 |
|
|
watchPath: watchPath,
|
469 |
|
|
entryPath: entryPath,
|
470 |
|
|
hasGlob: hasGlob,
|
471 |
|
|
globFilter: globFilter,
|
472 |
|
|
filterPath: filterPath,
|
473 |
|
|
filterDir: filterDir
|
474 |
|
|
};
|
475 |
|
|
};
|
476 |
|
|
|
477 |
|
|
// Directory helpers
|
478 |
|
|
// -----------------
|
479 |
|
|
|
480 |
|
|
// Private method: Provides directory tracking objects
|
481 |
|
|
//
|
482 |
|
|
// * directory - string, path of the directory
|
483 |
|
|
//
|
484 |
|
|
// Returns the directory's tracking object
|
485 |
|
|
FSWatcher.prototype._getWatchedDir = function(directory) {
|
486 |
|
|
var dir = sysPath.resolve(directory);
|
487 |
|
|
var watcherRemove = this._remove.bind(this);
|
488 |
|
|
if (!(dir in this._watched)) this._watched[dir] = {
|
489 |
|
|
_items: Object.create(null),
|
490 |
|
|
add: function(item) {
|
491 |
|
|
if (item !== '.' && item !== '..') this._items[item] = true;
|
492 |
|
|
},
|
493 |
|
|
remove: function(item) {
|
494 |
|
|
delete this._items[item];
|
495 |
|
|
if (!this.children().length) {
|
496 |
|
|
fs.readdir(dir, function(err) {
|
497 |
|
|
if (err) watcherRemove(sysPath.dirname(dir), sysPath.basename(dir));
|
498 |
|
|
});
|
499 |
|
|
}
|
500 |
|
|
},
|
501 |
|
|
has: function(item) {return item in this._items;},
|
502 |
|
|
children: function() {return Object.keys(this._items);}
|
503 |
|
|
};
|
504 |
|
|
return this._watched[dir];
|
505 |
|
|
};
|
506 |
|
|
|
507 |
|
|
// File helpers
|
508 |
|
|
// ------------
|
509 |
|
|
|
510 |
|
|
// Private method: Check for read permissions
|
511 |
|
|
// Based on this answer on SO: http://stackoverflow.com/a/11781404/1358405
|
512 |
|
|
//
|
513 |
|
|
// * stats - object, result of fs.stat
|
514 |
|
|
//
|
515 |
|
|
// Returns boolean
|
516 |
|
|
FSWatcher.prototype._hasReadPermissions = function(stats) {
|
517 |
|
|
return Boolean(4 & parseInt(((stats && stats.mode) & 0x1ff).toString(8)[0], 10));
|
518 |
|
|
};
|
519 |
|
|
|
520 |
|
|
// Private method: Handles emitting unlink events for
|
521 |
|
|
// files and directories, and via recursion, for
|
522 |
|
|
// files and directories within directories that are unlinked
|
523 |
|
|
//
|
524 |
|
|
// * directory - string, directory within which the following item is located
|
525 |
|
|
// * item - string, base path of item/directory
|
526 |
|
|
//
|
527 |
|
|
// Returns nothing
|
528 |
|
|
FSWatcher.prototype._remove = function(directory, item) {
|
529 |
|
|
// if what is being deleted is a directory, get that directory's paths
|
530 |
|
|
// for recursive deleting and cleaning of watched object
|
531 |
|
|
// if it is not a directory, nestedDirectoryChildren will be empty array
|
532 |
|
|
var path = sysPath.join(directory, item);
|
533 |
|
|
var fullPath = sysPath.resolve(path);
|
534 |
|
|
var isDirectory = this._watched[path] || this._watched[fullPath];
|
535 |
|
|
|
536 |
|
|
// prevent duplicate handling in case of arriving here nearly simultaneously
|
537 |
|
|
// via multiple paths (such as _handleFile and _handleDir)
|
538 |
|
|
if (!this._throttle('remove', path, 100)) return;
|
539 |
|
|
|
540 |
|
|
// if the only watched file is removed, watch for its return
|
541 |
|
|
var watchedDirs = Object.keys(this._watched);
|
542 |
|
|
if (!isDirectory && !this.options.useFsEvents && watchedDirs.length === 1) {
|
543 |
|
|
this.add(directory, item, true);
|
544 |
|
|
}
|
545 |
|
|
|
546 |
|
|
// This will create a new entry in the watched object in either case
|
547 |
|
|
// so we got to do the directory check beforehand
|
548 |
|
|
var nestedDirectoryChildren = this._getWatchedDir(path).children();
|
549 |
|
|
|
550 |
|
|
// Recursively remove children directories / files.
|
551 |
|
|
nestedDirectoryChildren.forEach(function(nestedItem) {
|
552 |
|
|
this._remove(path, nestedItem);
|
553 |
|
|
}, this);
|
554 |
|
|
|
555 |
|
|
// Check if item was on the watched list and remove it
|
556 |
|
|
var parent = this._getWatchedDir(directory);
|
557 |
|
|
var wasTracked = parent.has(item);
|
558 |
|
|
parent.remove(item);
|
559 |
|
|
|
560 |
|
|
// If we wait for this file to be fully written, cancel the wait.
|
561 |
|
|
var relPath = path;
|
562 |
|
|
if (this.options.cwd) relPath = sysPath.relative(this.options.cwd, path);
|
563 |
|
|
if (this.options.awaitWriteFinish && this._pendingWrites[relPath]) {
|
564 |
|
|
var event = this._pendingWrites[relPath].cancelWait();
|
565 |
|
|
if (event === 'add') return;
|
566 |
|
|
}
|
567 |
|
|
|
568 |
|
|
// The Entry will either be a directory that just got removed
|
569 |
|
|
// or a bogus entry to a file, in either case we have to remove it
|
570 |
|
|
delete this._watched[path];
|
571 |
|
|
delete this._watched[fullPath];
|
572 |
|
|
var eventName = isDirectory ? 'unlinkDir' : 'unlink';
|
573 |
|
|
if (wasTracked && !this._isIgnored(path)) this._emit(eventName, path);
|
574 |
|
|
|
575 |
|
|
// Avoid conflicts if we later create another file with the same name
|
576 |
|
|
if (!this.options.useFsEvents) {
|
577 |
|
|
this._closePath(path);
|
578 |
|
|
}
|
579 |
|
|
};
|
580 |
|
|
|
581 |
|
|
FSWatcher.prototype._closePath = function(path) {
|
582 |
|
|
if (!this._closers[path]) return;
|
583 |
|
|
this._closers[path].forEach(function(closer) {
|
584 |
|
|
closer();
|
585 |
|
|
});
|
586 |
|
|
delete this._closers[path];
|
587 |
|
|
this._getWatchedDir(sysPath.dirname(path)).remove(sysPath.basename(path));
|
588 |
|
|
}
|
589 |
|
|
|
590 |
|
|
// Public method: Adds paths to be watched on an existing FSWatcher instance
|
591 |
|
|
|
592 |
|
|
// * paths - string or array of strings, file/directory paths and/or globs
|
593 |
|
|
// * _origAdd - private boolean, for handling non-existent paths to be watched
|
594 |
|
|
// * _internal - private boolean, indicates a non-user add
|
595 |
|
|
|
596 |
|
|
// Returns an instance of FSWatcher for chaining.
|
597 |
|
|
FSWatcher.prototype.add = function(paths, _origAdd, _internal) {
|
598 |
|
|
var disableGlobbing = this.options.disableGlobbing;
|
599 |
|
|
var cwd = this.options.cwd;
|
600 |
|
|
this.closed = false;
|
601 |
|
|
paths = flatten(arrify(paths));
|
602 |
|
|
|
603 |
|
|
if (!paths.every(isString)) {
|
604 |
|
|
throw new TypeError('Non-string provided as watch path: ' + paths);
|
605 |
|
|
}
|
606 |
|
|
|
607 |
|
|
if (cwd) paths = paths.map(function(path) {
|
608 |
|
|
var absPath;
|
609 |
|
|
if (isAbsolute(path)) {
|
610 |
|
|
absPath = path;
|
611 |
|
|
} else if (path[0] === '!') {
|
612 |
|
|
absPath = '!' + sysPath.join(cwd, path.substring(1));
|
613 |
|
|
} else {
|
614 |
|
|
absPath = sysPath.join(cwd, path);
|
615 |
|
|
}
|
616 |
|
|
|
617 |
|
|
// Check `path` instead of `absPath` because the cwd portion can't be a glob
|
618 |
|
|
if (disableGlobbing || !isGlob(path)) {
|
619 |
|
|
return absPath;
|
620 |
|
|
} else {
|
621 |
|
|
return normalizePath(absPath);
|
622 |
|
|
}
|
623 |
|
|
});
|
624 |
|
|
|
625 |
|
|
// set aside negated glob strings
|
626 |
|
|
paths = paths.filter(function(path) {
|
627 |
|
|
if (path[0] === '!') {
|
628 |
|
|
this._ignoredPaths[path.substring(1)] = true;
|
629 |
|
|
} else {
|
630 |
|
|
// if a path is being added that was previously ignored, stop ignoring it
|
631 |
|
|
delete this._ignoredPaths[path];
|
632 |
|
|
delete this._ignoredPaths[path + '/**'];
|
633 |
|
|
|
634 |
|
|
// reset the cached userIgnored anymatch fn
|
635 |
|
|
// to make ignoredPaths changes effective
|
636 |
|
|
this._userIgnored = null;
|
637 |
|
|
|
638 |
|
|
return true;
|
639 |
|
|
}
|
640 |
|
|
}, this);
|
641 |
|
|
|
642 |
|
|
if (this.options.useFsEvents && FsEventsHandler.canUse()) {
|
643 |
|
|
if (!this._readyCount) this._readyCount = paths.length;
|
644 |
|
|
if (this.options.persistent) this._readyCount *= 2;
|
645 |
|
|
paths.forEach(this._addToFsEvents, this);
|
646 |
|
|
} else {
|
647 |
|
|
if (!this._readyCount) this._readyCount = 0;
|
648 |
|
|
this._readyCount += paths.length;
|
649 |
|
|
asyncEach(paths, function(path, next) {
|
650 |
|
|
this._addToNodeFs(path, !_internal, 0, 0, _origAdd, function(err, res) {
|
651 |
|
|
if (res) this._emitReady();
|
652 |
|
|
next(err, res);
|
653 |
|
|
}.bind(this));
|
654 |
|
|
}.bind(this), function(error, results) {
|
655 |
|
|
results.forEach(function(item) {
|
656 |
|
|
if (!item || this.closed) return;
|
657 |
|
|
this.add(sysPath.dirname(item), sysPath.basename(_origAdd || item));
|
658 |
|
|
}, this);
|
659 |
|
|
}.bind(this));
|
660 |
|
|
}
|
661 |
|
|
|
662 |
|
|
return this;
|
663 |
|
|
};
|
664 |
|
|
|
665 |
|
|
// Public method: Close watchers or start ignoring events from specified paths.
|
666 |
|
|
|
667 |
|
|
// * paths - string or array of strings, file/directory paths and/or globs
|
668 |
|
|
|
669 |
|
|
// Returns instance of FSWatcher for chaining.
|
670 |
|
|
FSWatcher.prototype.unwatch = function(paths) {
|
671 |
|
|
if (this.closed) return this;
|
672 |
|
|
paths = flatten(arrify(paths));
|
673 |
|
|
|
674 |
|
|
paths.forEach(function(path) {
|
675 |
|
|
// convert to absolute path unless relative path already matches
|
676 |
|
|
if (!isAbsolute(path) && !this._closers[path]) {
|
677 |
|
|
if (this.options.cwd) path = sysPath.join(this.options.cwd, path);
|
678 |
|
|
path = sysPath.resolve(path);
|
679 |
|
|
}
|
680 |
|
|
|
681 |
|
|
this._closePath(path);
|
682 |
|
|
|
683 |
|
|
this._ignoredPaths[path] = true;
|
684 |
|
|
if (path in this._watched) {
|
685 |
|
|
this._ignoredPaths[path + '/**'] = true;
|
686 |
|
|
}
|
687 |
|
|
|
688 |
|
|
// reset the cached userIgnored anymatch fn
|
689 |
|
|
// to make ignoredPaths changes effective
|
690 |
|
|
this._userIgnored = null;
|
691 |
|
|
}, this);
|
692 |
|
|
|
693 |
|
|
return this;
|
694 |
|
|
};
|
695 |
|
|
|
696 |
|
|
// Public method: Close watchers and remove all listeners from watched paths.
|
697 |
|
|
|
698 |
|
|
// Returns instance of FSWatcher for chaining.
|
699 |
|
|
FSWatcher.prototype.close = function() {
|
700 |
|
|
if (this.closed) return this;
|
701 |
|
|
|
702 |
|
|
this.closed = true;
|
703 |
|
|
Object.keys(this._closers).forEach(function(watchPath) {
|
704 |
|
|
this._closers[watchPath].forEach(function(closer) {
|
705 |
|
|
closer();
|
706 |
|
|
});
|
707 |
|
|
delete this._closers[watchPath];
|
708 |
|
|
}, this);
|
709 |
|
|
this._watched = Object.create(null);
|
710 |
|
|
|
711 |
|
|
this.removeAllListeners();
|
712 |
|
|
return this;
|
713 |
|
|
};
|
714 |
|
|
|
715 |
|
|
// Public method: Expose list of watched paths
|
716 |
|
|
|
717 |
|
|
// Returns object w/ dir paths as keys and arrays of contained paths as values.
|
718 |
|
|
FSWatcher.prototype.getWatched = function() {
|
719 |
|
|
var watchList = {};
|
720 |
|
|
Object.keys(this._watched).forEach(function(dir) {
|
721 |
|
|
var key = this.options.cwd ? sysPath.relative(this.options.cwd, dir) : dir;
|
722 |
|
|
watchList[key || '.'] = Object.keys(this._watched[dir]._items).sort();
|
723 |
|
|
}.bind(this));
|
724 |
|
|
return watchList;
|
725 |
|
|
};
|
726 |
|
|
|
727 |
|
|
// Attach watch handler prototype methods
|
728 |
|
|
function importHandler(handler) {
|
729 |
|
|
Object.keys(handler.prototype).forEach(function(method) {
|
730 |
|
|
FSWatcher.prototype[method] = handler.prototype[method];
|
731 |
|
|
});
|
732 |
|
|
}
|
733 |
|
|
importHandler(NodeFsHandler);
|
734 |
|
|
if (FsEventsHandler.canUse()) importHandler(FsEventsHandler);
|
735 |
|
|
|
736 |
|
|
// Export FSWatcher class
|
737 |
|
|
exports.FSWatcher = FSWatcher;
|
738 |
|
|
|
739 |
|
|
// Public function: Instantiates watcher with paths to be tracked.
|
740 |
|
|
|
741 |
|
|
// * paths - string or array of strings, file/directory paths and/or globs
|
742 |
|
|
// * options - object, chokidar options
|
743 |
|
|
|
744 |
|
|
// Returns an instance of FSWatcher for chaining.
|
745 |
|
|
exports.watch = function(paths, options) {
|
746 |
|
|
return new FSWatcher(options).add(paths);
|
747 |
|
|
};
|