Skip to content

Commit

Permalink
Register Generators (as stubs) with resolved path (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoScabbiolo authored and SBoudrias committed Jun 23, 2018
1 parent 18d253d commit bec6f47
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
9 changes: 5 additions & 4 deletions lib/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,12 @@ class Environment extends EventEmitter {
* functions under the provided namespace. `registerStub` will enforce the function passed
* to extend the Base generator automatically.
*
* @param {Function} Generator - A Generator constructor or a simple function
* @param {String} namespace - Namespace under which register the generator
* @param {Function} Generator - A Generator constructor or a simple function
* @param {String} namespace - Namespace under which register the generator
* @param {String} [resolved] - The file path to the generator
* @return {this}
*/
registerStub(Generator, namespace) {
registerStub(Generator, namespace, resolved) {
if (typeof Generator !== 'function') {
return this.error(new Error('You must provide a stub function to register.'));
}
Expand All @@ -255,7 +256,7 @@ class Environment extends EventEmitter {
return this.error(new Error('You must provide a namespace to register.'));
}

this.store.add(namespace, Generator);
this.store.add(namespace, Generator, resolved);

return this;
}
Expand Down
13 changes: 7 additions & 6 deletions lib/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ class Store {

/**
* Store a module under the namespace key
* @param {String} namespace - The key under which the generator can be retrieved
* @param {String|Function} generator - A generator module or a module path
* @param {String} namespace - The key under which the generator can be retrieved
* @param {String|Function} generator - A generator module or a module path
* @param {String} [resolved] - The file path to the generator (used only if generator is a module)
*/
add(namespace, generator) {
add(namespace, generator, resolved) {
if (typeof generator === 'string') {
this._storeAsPath(namespace, generator);
return;
}

this._storeAsModule(namespace, generator);
this._storeAsModule(namespace, generator, resolved);
}

_storeAsPath(namespace, path) {
Expand All @@ -43,9 +44,9 @@ class Store {
});
}

_storeAsModule(namespace, Generator) {
_storeAsModule(namespace, Generator, resolved = 'unknown') {
this._meta[namespace] = {
resolved: 'unknown',
resolved,
namespace
};

Expand Down
8 changes: 7 additions & 1 deletion test/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,17 +322,23 @@ describe('Environment', () => {
describe('#registerStub()', () => {
beforeEach(function () {
this.simpleDummy = sinon.spy();
this.resolvedDummy = sinon.spy();
this.completeDummy = function () {};
util.inherits(this.completeDummy, Generator);
this.env
.registerStub(this.simpleDummy, 'dummy:simple')
.registerStub(this.completeDummy, 'dummy:complete');
.registerStub(this.completeDummy, 'dummy:complete')
.registerStub(this.resolvedDummy, 'dummy:resolved', 'dummy/path');
});

it('register a function under a namespace', function () {
assert.equal(this.completeDummy, this.env.get('dummy:complete'));
});

it('registers the resolved path', function () {
assert.equal('dummy/path', this.env.get('dummy:resolved').resolved);
});

it('throws if invalid generator', function () {
assert.throws(this.env.registerStub.bind(this.env, [], 'dummy'), /stub\sfunction/);
});
Expand Down
7 changes: 2 additions & 5 deletions test/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Store', () => {

describe('storing as module', () => {
beforeEach(function () {
this.store.add('foo:module', this.module);
this.store.add('foo:module', this.module, '/foo/path');
this.outcome = this.store.get('foo:module');
});

Expand All @@ -26,10 +26,7 @@ describe('Store', () => {

it('assign meta data to the module', function () {
assert.equal(this.outcome.namespace, 'foo:module');
});

it('assign dummy resolved value (can\'t determine the path of an instantiated)', function () {
assert.ok(this.outcome.resolved.length > 0);
assert.equal(this.outcome.resolved, '/foo/path');
});
});

Expand Down

0 comments on commit bec6f47

Please sign in to comment.