match.js
6.57 KB
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
const minimatch = require('minimatch');
const path = require('path');
const fs = require('fs');
const debug = require('debug')('nodemon:match');
const utils = require('../utils');
module.exports = match;
module.exports.rulesToMonitor = rulesToMonitor;
function rulesToMonitor(watch, ignore, config) {
var monitor = [];
if (!Array.isArray(ignore)) {
if (ignore) {
ignore = [ignore];
} else {
ignore = [];
}
}
if (!Array.isArray(watch)) {
if (watch) {
watch = [watch];
} else {
watch = [];
}
}
if (watch && watch.length) {
monitor = utils.clone(watch);
}
if (ignore) {
[].push.apply(monitor, (ignore || []).map(function (rule) {
return '!' + rule;
}));
}
var cwd = process.cwd();
// next check if the monitored paths are actual directories
// or just patterns - and expand the rule to include *.*
monitor = monitor.map(function (rule) {
var not = rule.slice(0, 1) === '!';
if (not) {
rule = rule.slice(1);
}
if (rule === '.' || rule === '.*') {
rule = '*.*';
}
var dir = path.resolve(cwd, rule);
try {
var stat = fs.statSync(dir);
if (stat.isDirectory()) {
rule = dir;
if (rule.slice(-1) !== '/') {
rule += '/';
}
rule += '**/*';
// `!not` ... sorry.
if (!not) {
config.dirs.push(dir);
}
} else {
// ensures we end up in the check that tries to get a base directory
// and then adds it to the watch list
throw new Error();
}
} catch (e) {
var base = tryBaseDir(dir);
if (!not && base) {
if (config.dirs.indexOf(base) === -1) {
config.dirs.push(base);
}
}
}
if (rule.slice(-1) === '/') {
// just slap on a * anyway
rule += '*';
}
// if the url ends with * but not **/* and not *.*
// then convert to **/* - somehow it was missed :-\
if (rule.slice(-4) !== '**/*' &&
rule.slice(-1) === '*' &&
rule.indexOf('*.') === -1) {
if (rule.slice(-2) !== '**') {
rule += '*/*';
}
}
return (not ? '!' : '') + rule;
});
return monitor;
}
function tryBaseDir(dir) {
var stat;
if (/[?*\{\[]+/.test(dir)) { // if this is pattern, then try to find the base
try {
var base = path.dirname(dir.replace(/([?*\{\[]+.*$)/, 'foo'));
stat = fs.statSync(base);
if (stat.isDirectory()) {
return base;
}
} catch (error) {
// console.log(error);
}
} else {
try {
stat = fs.statSync(dir);
// if this path is actually a single file that exists, then just monitor
// that, *specifically*.
if (stat.isFile() || stat.isDirectory()) {
return dir;
}
} catch (e) { }
}
return false;
}
function match(files, monitor, ext) {
// sort the rules by highest specificity (based on number of slashes)
// ignore rules (!) get sorted highest as they take precedent
const cwd = process.cwd();
var rules = monitor.sort(function (a, b) {
var r = b.split(path.sep).length - a.split(path.sep).length;
var aIsIgnore = a.slice(0, 1) === '!';
var bIsIgnore = b.slice(0, 1) === '!';
if (aIsIgnore || bIsIgnore) {
if (aIsIgnore) {
return -1;
}
return 1;
}
if (r === 0) {
return b.length - a.length;
}
return r;
}).map(function (s) {
var prefix = s.slice(0, 1);
if (prefix === '!') {
if (s.indexOf('!' + cwd) === 0) {
return s;
}
// if it starts with a period, then let's get the relative path
if (s.indexOf('!.') === 0) {
return '!' + path.resolve(cwd, s.substring(1));
}
return '!**' + (prefix !== path.sep ? path.sep : '') + s.slice(1);
}
// if it starts with a period, then let's get the relative path
if (s.indexOf('.') === 0) {
return path.resolve(cwd, s);
}
if (s.indexOf(cwd) === 0) {
return s;
}
return '**' + (prefix !== path.sep ? path.sep : '') + s;
});
debug('rules', rules);
var good = [];
var whitelist = []; // files that we won't check against the extension
var ignored = 0;
var watched = 0;
var usedRules = [];
var minimatchOpts = {
dot: true,
};
// enable case-insensitivity on Windows
if (utils.isWindows) {
minimatchOpts.nocase = true;
}
files.forEach(function (file) {
file = path.resolve(cwd, file);
var matched = false;
for (var i = 0; i < rules.length; i++) {
if (rules[i].slice(0, 1) === '!') {
if (!minimatch(file, rules[i], minimatchOpts)) {
debug('ignored', file, 'rule:', rules[i]);
ignored++;
matched = true;
break;
}
} else {
debug('matched', file, 'rule:', rules[i]);
if (minimatch(file, rules[i], minimatchOpts)) {
watched++;
// don't repeat the output if a rule is matched
if (usedRules.indexOf(rules[i]) === -1) {
usedRules.push(rules[i]);
utils.log.detail('matched rule: ' + rules[i]);
}
// if the rule doesn't match the WATCH EVERYTHING
// but *does* match a rule that ends with *.*, then
// white list it - in that we don't run it through
// the extension check too.
if (rules[i] !== '**' + path.sep + '*.*' &&
rules[i].slice(-3) === '*.*') {
whitelist.push(file);
} else if (path.basename(file) === path.basename(rules[i])) {
// if the file matches the actual rule, then it's put on whitelist
whitelist.push(file);
} else {
good.push(file);
}
matched = true;
break;
} else {
// utils.log.detail('no match: ' + rules[i], file);
}
}
}
if (!matched) {
ignored++;
}
});
debug('good', good)
// finally check the good files against the extensions that we're monitoring
if (ext) {
if (ext.indexOf(',') === -1) {
ext = '**/*.' + ext;
} else {
ext = '**/*.{' + ext + '}';
}
good = good.filter(function (file) {
// only compare the filename to the extension test
return minimatch(path.basename(file), ext, minimatchOpts);
});
} // else assume *.*
var result = good.concat(whitelist);
if (utils.isWindows) {
// fix for windows testing - I *think* this is okay to do
result = result.map(function (file) {
return file.slice(0, 1).toLowerCase() + file.slice(1);
});
}
return {
result: result,
ignored: ignored,
watched: watched,
total: files.length,
};
}