Skip to content

Commit

Permalink
Add returnNewGenerator option to composeWith (#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 4, 2020
1 parent f5a829d commit aebe760
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,9 @@ class Generator extends EventEmitter {
/**
* Compose this generator with another one.
* @param {String|Object|Array} generator The path to the generator module or an object (see examples)
* @param {Object} options The options passed to the Generator
* @return {this} This generator
* @param {Object} [options] The options passed to the Generator
* @param {boolean} [returnNewGenerator] Returns the created generator instead of returning this.
* @return {this|Object} This generator or the composed generator when returnNewGenerator=true
*
* @example <caption>Using a peerDependency generator</caption>
* this.composeWith('bootstrap', { sass: true });
Expand All @@ -669,7 +670,11 @@ class Generator extends EventEmitter {
* @example <caption>Passing a Generator class</caption>
* this.composeWith({ Generator: MyGenerator, path: '../generator-bootstrap/app/main.js' }, { sass: true });
*/
composeWith(generator, options) {
composeWith(generator, options, returnNewGenerator = false) {
if (typeof options === 'boolean') {
returnNewGenerator = options;
}

let instantiatedGenerator;

if (Array.isArray(generator)) {
Expand Down Expand Up @@ -763,6 +768,10 @@ class Generator extends EventEmitter {
this._composedWith.push(instantiatedGenerator);
}

if (returnNewGenerator) {
return instantiatedGenerator;
}

return this;
}

Expand Down
4 changes: 4 additions & 0 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,10 @@ describe('Base', () => {
this.env.registerStub(this.GenCompose, 'composed:gen');
});

it('returns the composed generator when pass true', function() {
assert(this.dummy.composeWith('composed:gen', true) instanceof this.GenCompose);
});

it('runs the composed generators', function(done) {
this.dummy.composeWith('composed:gen');

Expand Down

0 comments on commit aebe760

Please sign in to comment.