From 6aada9b5e752db0a92a503ab0171c49c7ded8e59 Mon Sep 17 00:00:00 2001 From: Tim Wood Date: Mon, 11 Nov 2013 16:00:28 -0800 Subject: [PATCH] Prevent the end event from firing multiple times. --- lib/actions/actions.js | 6 ++++-- test/env.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/actions/actions.js b/lib/actions/actions.js index 577448dd..46e915fe 100644 --- a/lib/actions/actions.js +++ b/lib/actions/actions.js @@ -119,7 +119,7 @@ function prepCopy(source, destination, process) { actions.copy = function copy(source, destination, process) { var file = prepCopy.call(this, source, destination, process); - + try { file.body = this.engine(file.body, this); } catch (err) { @@ -290,7 +290,9 @@ actions.checkForCollision = function checkForCollision(filepath, content, cb) { content: content }); - this.conflicter.once('resolved:' + filepath, cb.bind(this, null)); + this.conflicter.once('resolved:' + filepath, function (config) { + process.nextTick(cb.bind(this, null, config)); + }.bind(this)); }; /** diff --git a/test/env.js b/test/env.js index 38fc4781..dff4822e 100644 --- a/test/env.js +++ b/test/env.js @@ -501,6 +501,38 @@ describe('Environment', function () { // actual run .run('angular:all myapp'); }); + + it('only call the end event once (bug #402)', function (done) { + function GeneratorOnce() { + generators.Base.apply(this, arguments); + this.sourceRoot(path.join(__dirname, 'fixtures')); + this.destinationRoot(path.join(__dirname, 'temp')); + } + + util.inherits(GeneratorOnce, generators.Base); + + GeneratorOnce.prototype.createDuplicate = function () { + this.copy('foo-copy.js'); + this.copy('foo-copy.js'); + }; + + var generatorOnce = new GeneratorOnce([], { + env: generators(), + resolved: __filename + }); + + var isFirstEndEvent = true; + + generatorOnce.on('end', function () { + assert.ok(isFirstEndEvent); + if (isFirstEndEvent) { + done(); + } + isFirstEndEvent = false; + }); + + generatorOnce.run(); + }); }); describe('Store', function() {