diff --git a/lib/index.js b/lib/index.js index d4e12980..42b5f71e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1120,14 +1120,44 @@ class Generator extends Base { return this.env.runGenerator(this); } + /** + * Queue generator tasks. + * + * @return {Promise} + */ queueTasks() { + const beforeQueueCallback = + (this.features.taskPrefix && this.beforeQueue) || this._beforeQueue; + if (beforeQueueCallback) { + const maybePromise = beforeQueueCallback.call(this); + if (maybePromise && maybePromise.then) { + this.checkEnvironmentVersion('3.5.0'); + return maybePromise.then(() => this._queueTasks()); + } + } + + return this._queueTasks(); + } + + _queueTasks() { debug( `Queueing generator ${this.options.namespace} with generator version ${this.yoGeneratorVersion}` ); this.queueOwnTasks(); - for (const generator of this._composedWith) - this.env.queueGenerator(generator, false); + let promise; + for (const generator of this._composedWith) { + if (promise) { + promise.then(() => this.env.queueGenerator(generator, false)); + } else { + const maybePromise = this.env.queueGenerator(generator, false); + if (maybePromise.then) { + promise = maybePromise; + } + } + } + this._composedWith = []; + return promise; } /** diff --git a/test/base.js b/test/base.js index aa408be1..4f8b8d75 100644 --- a/test/base.js +++ b/test/base.js @@ -212,6 +212,7 @@ describe('Base', () => { beforeEach(function () { this.TestGenerator = class extends Base {}; _.extend(this.TestGenerator.prototype, { + _beforeQueue: sinon.spy(), exec: sinon.spy(), exec2: sinon.spy(), exec3: sinon.spy(), @@ -244,6 +245,11 @@ describe('Base', () => { assert.ok(this.testGen._running); }); + it('should call _beforeQueue', function () { + this.testGen.queueTasks(); + assert.ok(this.testGen._beforeQueue.calledOnce); + }); + it('run prototype methods (not instances one)', function () { this.testGen.exec = sinon.spy(); return this.testGen.run().then(() => { @@ -550,10 +556,11 @@ describe('Base', () => { }); }); - describe('#run() with', () => { + describe('#run() with task prefix', () => { beforeEach(function () { this.TestGenerator = class extends Base {}; _.extend(this.TestGenerator.prototype, { + beforeQueue: sinon.spy(), _private: sinon.spy(), '#composed': sinon.spy(), composed: sinon.spy(), @@ -581,6 +588,11 @@ describe('Base', () => { assert(this.TestGenerator.prototype.initializing.notCalled); assert(this.TestGenerator.prototype._private.notCalled); }); + + it('should call beforeQueue', function () { + this.testGen.queueTasks(); + assert.ok(this.testGen.beforeQueue.calledOnce); + }); }); describe('#argument()', () => {