Skip to content

Commit

Permalink
Add support to beforeQueue.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jul 5, 2021
1 parent 624f74e commit b8747da
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
34 changes: 32 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
14 changes: 13 additions & 1 deletion test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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()', () => {
Expand Down

0 comments on commit b8747da

Please sign in to comment.