Skip to content

Commit be88cc7

Browse files
committed
chore(build): watch logger should honor ignoreInitial option
It was confusing because the test.unit.dart task does ignore the initial, and the logger was hardcoded to always ignore the first task, leading to the appearance that a run was happening twice for no reason. Also, fixed the "fake ignoreInitial" handling to not rely on a fake event, which is not necessary. Closes angular#2101
1 parent ba07f39 commit be88cc7

3 files changed

Lines changed: 33 additions & 36 deletions

File tree

gulpfile.js

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,7 @@ function createDocsTasks(publicBuild) {
335335

336336
gulp.task(taskPrefix, [taskPrefix + '/assets', taskPrefix + '/app', taskPrefix + '/dgeni']);
337337
gulp.task(taskPrefix + '/watch', function() {
338-
return watch('docs/app/**/*', {
339-
ignoreInitial: false,
340-
log: watchLog
341-
}, [taskPrefix + '/app']);
338+
return watch('docs/app/**/*', [taskPrefix + '/app']);
342339
});
343340

344341
gulp.task(taskPrefix + '/test', function (done) {
@@ -439,7 +436,7 @@ gulp.task('test.unit.dart', function (done) {
439436
return;
440437
}
441438

442-
watch('modules/angular2/**', { ignoreInitial: true, log: watchLog }, [
439+
watch('modules/angular2/**', { ignoreInitial: true }, [
443440
'!build/tree.dart',
444441
'!test.unit.dart/karma-run'
445442
]);
@@ -488,7 +485,7 @@ gulp.task('test.unit.cjs', ['build/clean.js', 'build.tools'], function (neverDon
488485
'test.unit.cjs/ci'
489486
];
490487

491-
watch('modules/**', { ignoreInitial: false, log: watchLog }, buildAndTest);
488+
watch('modules/**', buildAndTest);
492489
});
493490

494491

@@ -506,10 +503,7 @@ gulp.task('test.unit.tools', ['build/clean.tools'], function(done) {
506503
'test.unit.tools/ci'
507504
];
508505

509-
watch(['tools/**', '!tools/**/test-fixtures/**'], {
510-
ignoreInitial: false,
511-
log: watchLog
512-
}, buildAndTest);
506+
watch(['tools/**', '!tools/**/test-fixtures/**'], buildAndTest);
513507
});
514508

515509

@@ -895,21 +889,3 @@ process.on('beforeExit', function() {
895889
beforeExitRan = true;
896890
gulp.start('cleanup.builder');
897891
});
898-
899-
function watchLog(triggerCount) {
900-
// Ignore initial event
901-
if (!--triggerCount) return;
902-
903-
process.stdout.write([
904-
'',
905-
'==================================================',
906-
' WATCH TRIGGERED BY FILE CHANGE #' + triggerCount,
907-
' On: ' + prettyTime(),
908-
'==================================================\n',
909-
].join('\n'));
910-
911-
function prettyTime() {
912-
var now = new Date();
913-
return now.toLocaleDateString() + " at " + now.toLocaleTimeString();
914-
}
915-
}

tools/build/watch.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ function watch(globs, opts, tasks) {
1212
var useRunSequence = typeof tasks !== 'function';
1313
var runTasks;
1414

15-
var log = typeof opts.log === 'function' ? opts.log : function noop() {};
16-
1715
if (useRunSequence) {
1816
if (!Array.isArray(tasks)) tasks = [tasks];
1917
tasks = tasks.slice();
@@ -50,6 +48,28 @@ function watch(globs, opts, tasks) {
5048
throw err;
5149
});
5250

51+
var log = function watchLogger(triggerCount) {
52+
// Don't report change for initial event
53+
if (!ignoreInitial && !--triggerCount) return;
54+
55+
process.stdout.write([
56+
'',
57+
'==================================================',
58+
' WATCH TRIGGERED BY FILE CHANGE #' + triggerCount,
59+
' On: ' + prettyTime(),
60+
'==================================================\n',
61+
].join('\n'));
62+
63+
function prettyTime() {
64+
var now = new Date();
65+
return now.toLocaleDateString() + " at " + now.toLocaleTimeString();
66+
}
67+
}
68+
69+
if (opts.log !== undefined && !opts.log) {
70+
log = function noopLog(triggerCount) {}
71+
}
72+
5373
var close = watcher.close.bind(watcher);
5474
watcher.close = function() {
5575
if (timeoutId !== null) clearTimeout(timeoutId);
@@ -60,7 +80,8 @@ function watch(globs, opts, tasks) {
6080
var timeoutId = null; // If non-null, event capture window is open
6181

6282
if (!ignoreInitial) {
63-
handleEvent('change', 'madeupFilePath'); // synthetic event to kick off the first task run
83+
// synthetic event to kick off the first task run
84+
timeoutId = setTimeout(invokeCallback, delay);
6485
}
6586

6687
return watcher;

tools/build/watch.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('watch()', function() {
2727

2828
it('should fire callback once for events which occur within `delay` window', function() {
2929
var cb = jasmine.createSpy('callback');
30-
watcher = watch('./$$fake_path/**/*', { delay: 10 }, cb);
30+
watcher = watch('./$$fake_path/**/*', { delay: 10, log: false }, cb);
3131

3232
watcher._emit('add', './$$fake_path/test.txt');
3333
timeout.flush(9);
@@ -61,7 +61,7 @@ describe('watch()', function() {
6161
expect(timeout.pending).toBe(1);
6262
}
6363

64-
var watcher = watch('./$$fake_path/**/*', { delay: 10 }, cb);
64+
var watcher = watch('./$$fake_path/**/*', { delay: 10, log: false }, cb);
6565

6666
watcher._emit('change', './$$fake_path/test1.txt');
6767
expect(timeout.pending).toBe(1);
@@ -81,7 +81,7 @@ describe('watch()', function() {
8181
done();
8282
}
8383

84-
var watcher = watch('./$$fake_path/**/*', { delay: 10 }, cb);
84+
var watcher = watch('./$$fake_path/**/*', { delay: 10, log: false }, cb);
8585

8686
watcher._emit('change', './$$fake_path/test1.txt');
8787
timeout.flush();
@@ -96,7 +96,7 @@ describe('watch()', function() {
9696

9797
it('should cancel pending callback if FSWatcher is closed', function() {
9898
var cb = jasmine.createSpy('callback');
99-
var watcher = watch('./$$fake_path/**/*', { delay: 10 }, cb);
99+
var watcher = watch('./$$fake_path/**/*', { delay: 10, log: false }, cb);
100100

101101
watcher._emit('change', './$$fake_path/test1.txt');
102102
expect(timeout.pending).toBe(1);
@@ -119,7 +119,7 @@ describe('watch()', function() {
119119
expect(timeout.pending).toBe(0);
120120
}
121121

122-
var watcher = watch('./$$fake_path/**/*', { delay: 10 }, cb);
122+
var watcher = watch('./$$fake_path/**/*', { delay: 10, log: false }, cb);
123123
watcher._emit('change', './$$fake_path/test1.txt');
124124

125125
timeout.flush(10);

0 commit comments

Comments
 (0)