-
Notifications
You must be signed in to change notification settings - Fork 180
/
watch.js
81 lines (70 loc) · 2.12 KB
/
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
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
/*
* watch.js: Plugin for `Monitor` instances which adds file watching.
*
* (C) 2010 Charlie Robbins & the Contributors
* MIT LICENCE
*
*/
var fs = require('fs'),
path = require('path'),
minimatch = require('minimatch'),
chokidar = require('chokidar');
exports.name = 'watch';
//
// ### @private function _watchFilter
// #### @file {string} File name
// Determines whether we should restart if `file` change (@mikeal's filtering
// is pretty messed up).
//
function watchFilter(fileName) {
var relFileName = path.relative(this.watchDirectory, fileName),
length = this.watchIgnorePatterns.length,
testName,
i;
if (this.watchIgnoreDotFiles && path.basename(fileName)[0] === '.') {
return false;
}
for (i = 0; i < length; i++) {
if (this.watchIgnorePatterns[i].length > 0) {
testName = (this.watchIgnorePatterns[i].charAt(0) !== '/') ? relFileName : fileName;
if (minimatch(testName, this.watchIgnorePatterns[i], { matchBase: this.watchDirectory })) {
return false;
}
}
}
return true;
}
//
// ### function attach (options)
// #### @options {Object} Options for attaching to `Monitor`
//
// Attaches functionality for logging stdout and stderr to `Monitor` instances.
//
exports.attach = function () {
var watchDirectory = this.watchDirectory,
monitor = this;
fs.readFile(path.join(this.watchDirectory, '.foreverignore'), 'utf8', function (err, data) {
if (err) {
return monitor.emit('watch:error', {
message: 'Could not read .foreverignore file.',
error: err.message
});
}
Array.prototype.push.apply(monitor.watchIgnorePatterns, data.split('\n').filter(Boolean));
});
var opts = {
usePolling: this.usePolling,
interval: this.pollingInterval,
ignoreInitial: true,
ignored: function(fileName) {
return !watchFilter.call(monitor, fileName);
}
};
// Or, ignore: function(fileName) { return !watchFilter(fileName) }
chokidar
.watch(this.watchDirectory, opts)
.on('all', function(f, stat) {
monitor.emit('watch:restart', { file: f, stat: stat });
monitor.restart();
});
};