Skip to content
This repository has been archived by the owner on Jul 23, 2023. It is now read-only.

Invoke build with change information #8

Merged
merged 1 commit into from
Dec 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ function Watcher(builder, options) {
this.options.filter = this.options.filter || defaultFilterFunction;
this.watched = Object.create(null);
this.timeout = null;
this.changedFiles = [];
this.sequence = this.build();
}

Watcher.prototype = Object.create(EventEmitter.prototype);

// gathers rapid changes as one build
Watcher.prototype.scheduleBuild = function (filePath) {
this.changedFiles.push(filePath);
if (this.timeout) {
logger.info('debounce scheduleBuild: %s', filePath);
return;
Expand Down Expand Up @@ -98,8 +100,20 @@ Watcher.prototype.build = function Watcher_build(filePath) {
return hash;
}

var changedFiles = this.changedFiles;
this.changedFiles = [];

var annotation = {
type: !!filePath ? 'rebuild' : 'initial',
reason: 'watcher',
primaryFile: filePath,
changedFiles: changedFiles,
};

logger.info('%o', annotation);

return this.builder
.build(addWatchDir)
.build(addWatchDir, annotation)
.then(saveNode)
.then(totalTime)
.then(appendFilePath)
Expand Down Expand Up @@ -182,3 +196,4 @@ Watcher.prototype.close = function () {
Watcher.prototype.then = function(success, fail) {
return this.sequence.then(success, fail);
};
;
58 changes: 58 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var fs = require('fs');
var path = require('path');
var broccoli = require('ember-cli-broccoli');
var rimraf = require('rimraf');
var chai = require('chai'), expect = chai.expect;
Expand Down Expand Up @@ -201,4 +202,61 @@ describe('broccoli-sane-watcher', function () {

return defer.promise;
});

it('calls `build` with an annotation explaining why a build is occurring', function() {
var defer = RSVP.defer();

fs.mkdirSync('tests/fixtures/a');
var filter = new TestFilter(['tests/fixtures/a'], function () {
return 'output';
});

var buildArgs = null;
var builder = new broccoli.Builder(filter);
builder.build = (function (originalBuild) {
return function () {
buildArgs = Array.prototype.slice.call(arguments);
return originalBuild.apply(builder, buildArgs);
}
})(builder.build);
watcher = new Watcher(builder);

watcher.on('error', errorHandler(defer));

var builds = 0;

watcher.on('change', eventHandler(function () {
var build = ++builds;
if (build === 1) {
fs.writeFileSync('tests/fixtures/a/foo.js');
fs.writeFileSync('tests/fixtures/a/bar.js');
fs.writeFileSync('tests/fixtures/a/baz.js');
fs.mkdirSync('tests/fixtures/a/ohai');
fs.writeFileSync('tests/fixtures/a/ohai/foo.js');
} else {
expect(buildArgs.length).to.eql(2);
var annotation = buildArgs[1];
var expectedChangedFiles = [
path.join(__dirname, 'fixtures/a/bar.js'),
path.join(__dirname, 'fixtures/a/baz.js'),
path.join(__dirname, 'fixtures/a/foo.js'),
path.join(__dirname, 'fixtures/a/ohai'),
path.join(__dirname, 'fixtures/a/ohai/foo.js'),
];

expect(annotation.reason).to.eql('watcher');
expect(annotation.type).to.eql('rebuild');
// These are a bit fuzzy because there's a race and will vary from dev
// machines & CI &c.; but we expect at least 1 changed file and if we
// have more than each file twice we'd be very surprised.
expect(expectedChangedFiles).to.include.members(annotation.changedFiles);
expect(annotation.changedFiles.length).to.be.within(1, 10);
expect(expectedChangedFiles).to.include(annotation.primaryFile);

defer.resolve();
}
}, defer));

return defer.promise;
});
});