forked from gulpjs/glob-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
375 lines (300 loc) · 9.67 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
'use strict';
var fs = require('fs');
var path = require('path');
var expect = require('expect');
var sinon = require('sinon');
var rimraf = require('rimraf');
var through = require('through2');
var normalizePath = require('normalize-path');
var watch = require('../');
// Default delay on debounce
var timeout = 200;
describe('glob-watcher', function() {
var watcher;
var outDir = path.join(__dirname, './fixtures/');
var outFile1 = path.join(outDir, 'changed.js');
var outFile2 = path.join(outDir, 'added.js');
var globPattern = '**/*.js';
var outGlob = path.join(outDir, globPattern);
var singleAdd = path.join(outDir, 'changed.js');
var ignoreGlob = '!' + singleAdd;
function changeFile() {
fs.writeFileSync(outFile1, 'hello changed');
}
function addFile() {
fs.writeFileSync(outFile2, 'hello added');
}
beforeEach(function(cb) {
fs.mkdirSync(outDir);
fs.writeFileSync(outFile1, 'hello world');
cb();
});
afterEach(function(cb) {
if (watcher) {
watcher.close();
}
rimraf(outDir, cb);
});
after(function(cb) {
rimraf(outDir, cb);
});
it('only requires a glob and returns watcher', function(done) {
watcher = watch(outGlob);
watcher.once('change', function(filepath) {
expect(filepath).toEqual(outFile1);
done();
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', changeFile);
});
it('picks up added files', function(done) {
watcher = watch(outGlob);
watcher.once('add', function(filepath) {
expect(filepath).toEqual(outFile2);
done();
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', addFile);
});
it('works with OS-specific cwd', function(done) {
watcher = watch('./fixtures/' + globPattern, { cwd: __dirname });
watcher.once('change', function(filepath) {
// Uses path.join here because the resulting path is OS-specific
expect(filepath).toEqual(path.join('fixtures', 'changed.js'));
done();
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', changeFile);
});
it('accepts a callback & calls when file is changed', function(done) {
watcher = watch(outGlob, function(cb) {
cb();
done();
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', changeFile);
});
it('accepts a callback & calls when file is added', function(done) {
watcher = watch(outGlob, function(cb) {
cb();
done();
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', addFile);
});
it('waits for completion is signaled before running again', function(done) {
var runs = 0;
watcher = watch(outGlob, function(cb) {
runs++;
if (runs === 1) {
setTimeout(function() {
expect(runs).toEqual(1);
cb();
}, timeout * 3);
}
if (runs === 2) {
cb();
done();
}
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', function() {
changeFile();
// Fire after double the delay
setTimeout(changeFile, timeout * 2);
});
});
// It can signal completion with anything async-done supports
// Just wanted to have a smoke test for streams
it('can signal completion with a stream', function(done) {
var runs = 0;
watcher = watch(outGlob, function(cb) {
runs++;
if (runs === 1) {
var stream = through();
setTimeout(function() {
expect(runs).toEqual(1);
stream.end();
}, timeout * 3);
return stream;
}
if (runs === 2) {
cb();
done();
}
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', function() {
changeFile();
// Fire after double the delay
setTimeout(changeFile, timeout * 2);
});
});
it('emits an error if one occurs in the callback and handler attached', function(done) {
var expectedError = new Error('boom');
watcher = watch(outGlob, function(cb) {
cb(expectedError);
});
watcher.on('error', function(err) {
expect(err).toEqual(expectedError);
done();
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', changeFile);
});
it('does not emit an error (and crash) when no handlers attached', function(done) {
var expectedError = new Error('boom');
watcher = watch(outGlob, function(cb) {
cb(expectedError);
setTimeout(done, timeout * 3);
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', changeFile);
});
it('allows the user to disable queueing', function(done) {
var runs = 0;
watcher = watch(outGlob, { queue: false }, function(cb) {
runs++;
setTimeout(function() {
// Expect 1 because run 2 is never queued
expect(runs).toEqual(1);
cb();
done();
}, timeout * 3);
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', function() {
changeFile();
// This will never trigger a call because queueing is disabled
setTimeout(changeFile, timeout * 2);
});
});
it('allows the user to adjust delay', function(done) {
var runs = 0;
watcher = watch(outGlob, { delay: (timeout / 2) }, function(cb) {
runs++;
if (runs === 1) {
setTimeout(function() {
expect(runs).toEqual(1);
cb();
}, timeout * 3);
}
if (runs === 2) {
expect(runs).toEqual(2);
cb();
done();
}
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', function() {
changeFile();
// This will queue because delay is halved
setTimeout(changeFile, timeout);
});
});
it('passes options to chokidar', function(done) {
// Callback is called while chokidar is discovering file paths
// if ignoreInitial is explicitly set to false and passed to chokidar
watcher = watch(outGlob, { ignoreInitial: false }, function(cb) {
cb();
done();
});
});
it('does not override default values with null values', function(done) {
watcher = watch(outGlob, { ignoreInitial: null }, function(cb) {
cb();
done();
});
// We default `ignoreInitial` to true and it isn't overwritten by null
// So wait for `on('ready')`
watcher.on('ready', changeFile);
});
it('watches exactly the given event', function(done) {
var spy = sinon.spy(function(cb) {
cb();
expect(spy.callCount).toEqual(1);
setTimeout(done, 500);
changeFile();
});
watcher = watch(outGlob, { events: 'add' }, spy);
watcher.on('ready', addFile);
});
it('accepts multiple events to watch', function(done) {
var spy = sinon.spy(function(cb) {
cb();
if (spy.callCount === 2) {
done();
}
});
watcher = watch(outGlob, { events: ['add', 'change'] }, spy);
watcher.on('ready', addFile);
watcher.on('add', function() {
setTimeout(changeFile, 500);
});
});
it('can ignore a glob after it has been added', function(done) {
watcher = watch([outGlob, ignoreGlob]);
watcher.once('change', function(filepath) {
// It should never reach here
expect(filepath).toNotExist();
done();
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', changeFile);
setTimeout(done, 1500);
});
it('can re-add a glob after it has been negated', function(done) {
watcher = watch([outGlob, ignoreGlob, singleAdd]);
watcher.once('change', function(filepath) {
expect(filepath).toEqual(singleAdd);
done();
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', changeFile);
});
it('can re-add a glob after it has been negated (unix style path)', function(done) {
watcher = watch([outGlob, ignoreGlob, normalizePath(singleAdd)]);
watcher.once('change', function(filepath) {
expect(filepath).toEqual(singleAdd);
done();
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', changeFile);
});
it('does not mutate the globs array', function(done) {
var globs = [outGlob, ignoreGlob, singleAdd];
watcher = watch(globs);
expect(globs[0]).toEqual(outGlob);
expect(globs[1]).toEqual(ignoreGlob);
expect(globs[2]).toEqual(singleAdd);
done();
});
it('passes ignores through to chokidar', function(done) {
var ignored = [singleAdd];
watcher = watch(outGlob, {
ignored: ignored,
});
watcher.once('change', function(filepath) {
// It should never reach here
expect(filepath).toNotExist();
done();
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', changeFile);
// Just test the non-mutation in this test
expect(ignored.length).toEqual(1);
setTimeout(done, 1500);
});
// https://github.com/gulpjs/glob-watcher/issues/46
it('ignoring globs also works with `cwd` option', function(done) {
watcher = watch(['fixtures/**', '!fixtures/*.js'], { cwd: 'test' });
watcher.once('change', function(filepath) {
// It should never reach here
expect(filepath).toNotExist();
done();
});
// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', changeFile);
setTimeout(done, 1500);
});
});