Skip to content

Commit fedb2fb

Browse files
committedDec 2, 2016
Update composeWith to take path or namespace as first argument - Fix #983
1 parent 1885dec commit fedb2fb

File tree

6 files changed

+18
-41
lines changed

6 files changed

+18
-41
lines changed
 

‎lib/index.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -491,14 +491,11 @@ Base.prototype.run = function run(cb) {
491491
* this.composeWith('bootstrap', { sass: true });
492492
*
493493
* @example <caption>Using a direct dependency generator</caption>
494-
* this.composeWith('bootstrap', { sass: true }, {
495-
* local: require.resolve('generator-bootstrap/app/main.js')
496-
* });
494+
* this.composeWith(require.resolve('generator-bootstrap/app/main.js'), { sass: true });
497495
*/
498496

499-
Base.prototype.composeWith = function composeWith(namespace, options, settings) {
497+
Base.prototype.composeWith = function composeWith(modulePath, options) {
500498
var generator;
501-
settings = settings || {};
502499
options = options || {};
503500

504501
// Pass down the default options so they're correclty mirrored down the chain.
@@ -509,13 +506,17 @@ Base.prototype.composeWith = function composeWith(namespace, options, settings)
509506
'skip-cache': this.options.skipCache
510507
}, options);
511508

512-
if (settings.local) {
513-
var Generator = require(settings.local);
514-
Generator.resolved = require.resolve(settings.local);
515-
Generator.namespace = namespace;
509+
try {
510+
var Generator = require(modulePath);
511+
Generator.resolved = require.resolve(modulePath);
512+
Generator.namespace = this.env.namespace(modulePath);
516513
generator = this.env.instantiate(Generator, {options: options});
517-
} else {
518-
generator = this.env.create(namespace, {options: options});
514+
} catch (err) {
515+
if (err.code === 'MODULE_NOT_FOUND') {
516+
generator = this.env.create(modulePath, {options: options});
517+
} else {
518+
throw err;
519+
}
519520
}
520521

521522
if (this._running) {

‎test/base.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -792,25 +792,21 @@ describe('Base', function () {
792792
describe('when passing a local path to a generator', function () {
793793
beforeEach(function () {
794794
this.spy = sinon.spy();
795-
this.stubPath = path.join(__dirname, 'fixtures/mocha-generator');
795+
this.stubPath = path.join(__dirname, 'fixtures/generator-mocha');
796796
this.LocalDummy = Base.extend({ exec: this.spy });
797797
mockery.registerMock(this.stubPath, this.LocalDummy);
798798
});
799799

800800
it('runs the composed generator', function (done) {
801-
this.dummy.composeWith('dumb', {}, { local: this.stubPath });
801+
this.dummy.composeWith(this.stubPath, {});
802802
this.dummy.run(function () {
803803
assert(this.LocalDummy.prototype.exec.called);
804804
done();
805805
}.bind(this));
806806
});
807807

808808
it('pass options and arguments to the composed generators', function (done) {
809-
this.dummy.composeWith(
810-
'dumb',
811-
{ foo: 'bar', 'skip-install': true },
812-
{ local: this.stubPath }
813-
);
809+
this.dummy.composeWith(this.stubPath, { foo: 'bar', 'skip-install': true });
814810

815811
this.dummy.run(function () {
816812
assert.equal(this.spy.firstCall.thisValue.options.foo, 'bar');
@@ -819,9 +815,9 @@ describe('Base', function () {
819815
});
820816

821817
it('sets correct metadata on the Generator constructor', function (done) {
822-
this.dummy.composeWith('dumb', {}, { local: this.stubPath });
818+
this.dummy.composeWith(this.stubPath, {});
823819
this.dummy.run(function () {
824-
assert.equal(this.spy.firstCall.thisValue.options.namespace, 'dumb');
820+
assert.equal(this.spy.firstCall.thisValue.options.namespace, 'mocha');
825821
assert.equal(
826822
this.spy.firstCall.thisValue.options.resolved,
827823
require.resolve(this.stubPath)
File renamed without changes.

‎test/fixtures/mocha-generator-base/package.json ‎test/fixtures/generator-mocha/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"author": "",
3-
"name": "mocha-generator",
3+
"name": "generator-mocha",
44
"version": "0.0.0",
55
"main": "main.js",
66
"dependencies": {},

‎test/fixtures/mocha-generator-base/main.js

-8
This file was deleted.

‎test/fixtures/mocha-generator/package.json

-12
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.