-
Notifications
You must be signed in to change notification settings - Fork 180
/
watch.js
112 lines (98 loc) · 2.9 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
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
/*
* watch.js: Plugin for `Monitor` instances which adds file watching.
*
* (C) 2010 Charlie Robbins & the Contributors
* MIT LICENCE
*
*/
const fs = require('fs'),
path = require('path'),
minimatch = require('minimatch'),
chokidar =
parseInt(process.versions.node) >= 8
? require('chokidar')
: require('@nicolo-ribaudo/chokidar-2');
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) {
let watchDirectory = this.watchDirectory, //this is array if multiple --watchDirectory options given, treat as array always
result = true;
if (!Array.isArray(watchDirectory)) {
watchDirectory = [watchDirectory];
}
watchDirectory.forEach(
function(directory) {
const relFileName = path.relative(directory, fileName),
length = this.watchIgnorePatterns.length;
let testName, i;
if (this.watchIgnoreDotFiles && path.basename(fileName)[0] === '.') {
result = 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: directory,
})
) {
result = false;
}
}
}
}.bind(this)
);
return result;
}
//
// ### function attach (options)
// #### @options {Object} Options for attaching to `Monitor`
//
// Attaches functionality for logging stdout and stderr to `Monitor` instances.
//
exports.attach = function() {
let watchDirectory = this.watchDirectory;
const //this.watchDirectory is array if multiple --watchDirectory options given, treat as array always
monitor = this;
if (!Array.isArray(watchDirectory)) {
watchDirectory = [watchDirectory];
}
watchDirectory.forEach(function(directory) {
fs.readFile(path.join(directory, '.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)
);
});
});
const 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(event, f, stat) {
monitor.emit('watch:restart', { file: f, stat: stat });
monitor.restart();
});
};