forked from Experience-Monks/devtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-watch.js
43 lines (36 loc) · 989 Bytes
/
file-watch.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
// a thin wrapper around chokidar file watching files
var chokidar = require('chokidar');
var assign = require('object-assign');
var EventEmitter = require('events').EventEmitter;
var ignores = [
'node_modules/**', 'bower_components/**',
'.git', '.hg', '.svn', '.DS_Store',
'*.swp', 'thumbs.db', 'desktop.ini'
];
module.exports = function fileWatch (glob, opt) {
opt = assign({
ignored: ignores,
ignoreInitial: true
}, opt);
if (opt.poll) {
opt.usePolling = true;
}
var emitter = new EventEmitter();
var closed = false;
var ready = false;
var watcher = chokidar.watch(glob, opt);
watcher.on('change', function (file) {
emitter.emit('change', file);
});
// [email protected] only allows close after ready event
watcher.once('ready', function () {
ready = true;
if (closed) watcher.close();
});
emitter.close = function () {
if (closed) return;
if (ready) watcher.close();
closed = true;
};
return emitter;
};