-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
330 lines (304 loc) · 8.74 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
'use strict';
var debug = require('debug')('wt');
var path = require('path');
var fs = require('fs');
var Base = require('sdk-base');
var util = require('util');
var ndir = require('ndir');
module.exports = Watcher;
module.exports.Watcher = Watcher;
/**
* Watcher
*
* @param {Object} options
* - {Boolean} [ignoreNodeModules] ignore node_modules or not, default is `true`
* - {Boolean} [ignoreHidden] ignore hidden file or not, default is `true`
* - {Number} [rewatchInterval] auto rewatch root dir interval,
* default is `0`, don't rewatch.
* @param {Function} [done], watch all dirs done callback.
*/
function Watcher(options) {
Base.call(this);
// http://nodejs.org/dist/v0.11.12/docs/api/fs.html#fs_caveats
// The recursive option is currently supported on OS X.
// Only FSEvents supports this type of file watching
// so it is unlikely any additional platforms will be added soon.
options = options || {};
if (options.ignoreHidden === undefined || options.ignoreHidden === null) {
options.ignoreHidden = true;
}
if (options.ignoreNodeModules === undefined || options.ignoreNodeModules === null){
options.ignoreNodeModules = true;
}
this._ignoreHidden = !!options.ignoreHidden;
this._ignoreNodeModules = !!options.ignoreNodeModules;
this._rewatchInterval = options.rewatchInterval;
this.watchOptions = {
persistent: true,
recursive: false, // so we dont use this features
};
this._watchers = {};
this._rootDirs = [];
this._rewatchTimer = null;
if (this._rewatchInterval && typeof this._rewatchInterval === 'number') {
this._rewatchTimer = setInterval(this._rewatchRootDirs.bind(this), this._rewatchInterval);
debug('start rewatch timer every %dms', this._rewatchInterval);
}
}
Watcher.watch = function (dirs, options, done) {
// watch(dirs, done);
if (typeof options === 'function') {
done = options;
options = null;
}
var watcher = new Watcher(options).watch(dirs);
if (typeof done === 'function') {
var count = Array.isArray(dirs) ? dirs.length : 1;
watcher.on('watch', function () {
count--;
if (count === 0) {
done();
}
});
}
return watcher;
};
util.inherits(Watcher, Base);
var proto = Watcher.prototype;
proto.isWatching = function (dir) {
return !!this._watchers[dir];
};
/**
* Start watch dir(s)
*
* @param {Array|String} dirs: dir path or path list
* @return {this}
*/
proto.watch = function (dirs) {
if (!Array.isArray(dirs)) {
dirs = [dirs];
}
debug('watch(%j)', dirs);
for (var i = 0; i < dirs.length; i++) {
var dir = dirs[i];
this._watchDir(dir);
if (this._rootDirs.indexOf(dir) === -1) {
this._rootDirs.push(dir);
}
}
return this;
};
proto.unwatch = function (dirs) {
if (!Array.isArray(dirs)) {
dirs = [dirs];
}
for (var i = 0; i < dirs.length; i++) {
var dir = dirs[i];
this._unwatchDir(dir);
if (this._rootDirs.indexOf(dir) !== -1) {
// remove from root dirs
this._rootDirs.splice(this._rootDirs.indexOf(dir), 1);
}
}
return this;
};
proto.close = function () {
for (var k in this._watchers) {
this._watchers[k].close();
this._watchers[k].removeAllListeners();
this._watchers[k] = null;
}
this._watchers = {};
// 等待一个事件循环后再移除所有时间,确保 watcher 的 error 事件还能被捕获
setImmediate(function () {
this.removeAllListeners();
}.bind(this));
if (this._rewatchTimer) {
clearInterval(this._rewatchTimer);
this._rewatchTimer = null;
}
};
proto._rewatchRootDirs = function () {
for (var i = 0; i < this._rootDirs.length; i++) {
var dir = this._rootDirs[i];
// watcher missing, meaning dir was deleted
// try to rewatch again
this._watchDirIfExists(dir);
}
};
proto._watchDirIfExists = function (dir) {
var that = this;
fs.stat(dir, function (err, stat) {
debug('[watchDirIfExists] %s, error: %s, exists: %s', dir, err, !!stat);
if (stat && stat.isDirectory()) {
if (!that._watchers[dir]) {
// watch again!
that._watchDir(dir);
}
} else if (!stat) {
// not exists, close watcher
that._unwatchDir(dir);
}
});
};
proto._watchDir = function (dir) {
var watchers = this._watchers;
var that = this;
debug('walking %s...', dir);
ndir.walk(dir).on('dir', function (dirpath) {
if (path.basename(dirpath)[0] === '.' && that._ignoreHidden) {
debug('ignore hidden dir: %s', dirpath);
return;
}
if (path.basename(dirpath) === 'node_modules' && that._ignoreNodeModules){
debug('ignore node_modules dir: %s', dirpath);
return;
}
if (watchers[dirpath]) {
debug('%s exists', dirpath);
return;
}
debug('fs.watch(%s) start...', dirpath);
var watcher;
try {
watcher = fs.watch(dirpath, that.watchOptions, that._handle.bind(that, dirpath));
} catch (err) {
err.dir = dirpath;
that.emit('error', err);
return;
}
watchers[dirpath] = watcher;
watcher.once('error', that._onWatcherError.bind(that, dirpath));
}).on('error', function (err) {
err.dir = dir;
that.emit('error', err);
}).on('end', function () {
debug('_watchDir(%s) done', dir);
// debug('now watching %s', Object.keys(that._watchers));
that.emit('watch', dir);
});
return this;
};
proto._unwatchDir = function (dir) {
var watcher = this._watchers[dir];
debug('unwatch %s, watcher exists: %s', dir, !!watcher);
if (watcher) {
watcher.close();
watcher.removeAllListeners();
delete this._watchers[dir];
this.emit('unwatch', dir);
}
// should close all subdir watchers too
var subdirs = Object.keys(this._watchers);
for (var i = 0; i < subdirs.length; i++) {
var subdir = subdirs[i];
if (subdir.indexOf(dir + '/') === 0) {
// close subdir watcher
watcher = this._watchers[subdir];
watcher.close();
watcher.removeAllListeners();
delete this._watchers[subdir];
debug('close subdir %s watcher by %s', subdir, dir);
}
}
};
proto._onWatcherError = function (dir, err) {
this._unwatchDir(dir);
err.dir = dir;
this.emit('error', err);
};
proto._handle = function (root, event, name) {
var that = this;
if (!name) {
debug('[WARNING] event:%j, name:%j not exists on %s', root);
return;
}
if (name[0] === '.' && this._ignoreHidden) {
debug('ignore %s on %s/%s', event, root, name);
return;
}
if (name === 'node_modules' && this._ignoreNodeModules) {
debug('ignore %s on %s/%s', event, root, name);
return;
}
// check root stat
fs.exists(root, function (exists) {
if (!exists) {
debug('[handle] %s %s on %s, root not exists', event, name, root);
that._handleChange({
event: event,
path: path.join(root, name),
stat: null,
remove: true,
isDirectory: false,
isFile: false,
});
// linux event, dir self remove, will fire `rename with dir name itself`
if (that._watchers[root]) {
debug('[handle] fire root:%s %s by %s', root, event, name);
that._handleChange({
event: event,
path: root,
stat: null,
remove: true,
isDirectory: true,
isFile: false,
});
}
return;
}
// children change
debug('[handle] %s %s on %s, root exists', event, name, root);
var fullpath = path.join(root, name);
fs.stat(fullpath, function (err, stat) {
var info = {
event: event,
path: fullpath,
stat: stat,
remove: false,
isDirectory: stat && stat.isDirectory() || false,
isFile: stat && stat.isFile() || false,
};
if (err) {
if (err.code === 'ENOENT') {
info.remove = true;
}
}
if (event === 'change' && info.remove) {
// this should be a fs.watch bug
debug('[WARNING] %s on %s, but file not exists, ignore this', event, fullpath);
return;
}
that._handleChange(info);
});
});
};
proto._handleChange = function (info) {
debug('_handleChange(%j)', info);
var that = this;
if (info.remove) {
var watcher = that._watchers[info.path];
if (watcher) {
// close the exists watcher
info.isDirectory = true;
that._unwatchDir(info.path);
}
} else if (info.isDirectory) {
var watcher = that._watchers[info.path];
if (!watcher) {
// add new watcher
that._watchDir(info.path);
}
}
that.emit('all', info);
if (info.remove) {
debug('[remove event] %s, isDirectory: %s', info.path, info.isDirectory);
that.emit('remove', info);
} else if (info.isFile) {
debug('[file change event] %s', info.path);
that.emit('file', info);
} else if (info.isDirectory) {
debug('[dir change envet] %s', info.path);
that.emit('dir', info);
}
};