Skip to content

Commit

Permalink
Update composeWith to take path or namespace as first argument - Fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Dec 2, 2016
1 parent 1885dec commit fedb2fb
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 41 deletions.
23 changes: 12 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,14 +491,11 @@ Base.prototype.run = function run(cb) {
* this.composeWith('bootstrap', { sass: true });
*
* @example <caption>Using a direct dependency generator</caption>
* this.composeWith('bootstrap', { sass: true }, {
* local: require.resolve('generator-bootstrap/app/main.js')
* });
* this.composeWith(require.resolve('generator-bootstrap/app/main.js'), { sass: true });
*/

Base.prototype.composeWith = function composeWith(namespace, options, settings) {
Base.prototype.composeWith = function composeWith(modulePath, options) {
var generator;
settings = settings || {};
options = options || {};

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

if (settings.local) {
var Generator = require(settings.local);
Generator.resolved = require.resolve(settings.local);
Generator.namespace = namespace;
try {
var Generator = require(modulePath);
Generator.resolved = require.resolve(modulePath);
Generator.namespace = this.env.namespace(modulePath);
generator = this.env.instantiate(Generator, {options: options});
} else {
generator = this.env.create(namespace, {options: options});
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
generator = this.env.create(modulePath, {options: options});
} else {
throw err;
}
}

if (this._running) {
Expand Down
14 changes: 5 additions & 9 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,25 +792,21 @@ describe('Base', function () {
describe('when passing a local path to a generator', function () {
beforeEach(function () {
this.spy = sinon.spy();
this.stubPath = path.join(__dirname, 'fixtures/mocha-generator');
this.stubPath = path.join(__dirname, 'fixtures/generator-mocha');
this.LocalDummy = Base.extend({ exec: this.spy });
mockery.registerMock(this.stubPath, this.LocalDummy);
});

it('runs the composed generator', function (done) {
this.dummy.composeWith('dumb', {}, { local: this.stubPath });
this.dummy.composeWith(this.stubPath, {});
this.dummy.run(function () {
assert(this.LocalDummy.prototype.exec.called);
done();
}.bind(this));
});

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

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

it('sets correct metadata on the Generator constructor', function (done) {
this.dummy.composeWith('dumb', {}, { local: this.stubPath });
this.dummy.composeWith(this.stubPath, {});
this.dummy.run(function () {
assert.equal(this.spy.firstCall.thisValue.options.namespace, 'dumb');
assert.equal(this.spy.firstCall.thisValue.options.namespace, 'mocha');
assert.equal(
this.spy.firstCall.thisValue.options.resolved,
require.resolve(this.stubPath)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"author": "",
"name": "mocha-generator",
"name": "generator-mocha",
"version": "0.0.0",
"main": "main.js",
"dependencies": {},
Expand Down
8 changes: 0 additions & 8 deletions test/fixtures/mocha-generator-base/main.js

This file was deleted.

12 changes: 0 additions & 12 deletions test/fixtures/mocha-generator/package.json

This file was deleted.

0 comments on commit fedb2fb

Please sign in to comment.