Skip to content

Commit

Permalink
Save Generator Version in Environment
Browse files Browse the repository at this point in the history
  • Loading branch information
andersonkyle committed Oct 24, 2017
1 parent 9389040 commit af55d79
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class Environment extends EventEmitter {
* @param {String} namespace - Namespace under which register the generator (optional)
* @return {String} namespace - Namespace assigned to the registered generator
*/
register(name, namespace) {
register(name, namespace, packageJson) {
if (typeof name !== 'string') {
return this.error(new Error('You must provide a generator name to register.'));
}
Expand All @@ -231,7 +231,7 @@ class Environment extends EventEmitter {
return this.error(new Error('Unable to determine namespace.'));
}

this.store.add(namespace, modulePath);
this.store.add(namespace, modulePath, packageJson);

debug('Registered %s (%s)', namespace, modulePath);
return this;
Expand Down
26 changes: 16 additions & 10 deletions lib/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ resolver.lookup = function (cb) {
const patterns = [];

for (const lookup of this.lookups) {
for (const modulePath of generatorsModules) {
patterns.push(path.join(modulePath, lookup));
for (const module of generatorsModules) {
patterns.push(Object.assign({}, module, {path: path.join(module.path, lookup)}));
}
}

for (const pattern of patterns) {
for (const filename of globby.sync('*/index.js', {cwd: pattern})) {
this._tryRegistering(path.join(pattern, filename));
for (const filename of globby.sync('*/index.js', {cwd: pattern.path})) {
this._tryRegistering(Object.assign({}, pattern, {path: path.join(pattern.path, filename)}));
}
}

Expand Down Expand Up @@ -72,7 +72,11 @@ resolver.findGeneratorsIn = function (searchPaths) {
'generator-*',
'@*/generator-*'
], {cwd: root})
.map(match => path.join(root, match))
.map(match => {
const generatorPath = path.join(root, match);
const packageJson = path.join(generatorPath, 'package.json');
return {path: generatorPath, packageJson};
})
.concat(modules);
}

Expand All @@ -86,16 +90,18 @@ resolver.findGeneratorsIn = function (searchPaths) {
*/
resolver._tryRegistering = function (generatorReference) {
let namespace;
const realPath = fs.realpathSync(generatorReference);
const generatorPath = generatorReference.path;
const packageJson = generatorReference.packageJson;
const realPath = fs.realpathSync(generatorPath);

try {
debug('found %s, trying to register', generatorReference);
debug('found %s, trying to register', generatorPath);

if (realPath !== generatorReference) {
namespace = this.namespace(generatorReference);
if (realPath !== generatorPath) {
namespace = this.namespace(generatorPath);
}

this.register(realPath, namespace);
this.register(realPath, namespace, packageJson);
} catch (err) {
console.error('Unable to register %s (Error: %s)', generatorReference, err.message);
}
Expand Down
22 changes: 15 additions & 7 deletions lib/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ class Store {
* @param {String} namespace - The key under which the generator can be retrieved
* @param {String|Function} generator - A generator module or a module path
*/
add(namespace, generator) {
add(namespace, generator, packageJson) {
if (typeof generator === 'string') {
this._storeAsPath(namespace, generator);
this._storeAsPath(namespace, generator, packageJson);
return;
}

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

_storeAsPath(namespace, path) {
_storeAsPath(namespace, path, packageJson) {
this._meta[namespace] = {
resolved: path,
namespace
namespace,
packageJson,
get version() {
return require(packageJson).version;
}
};

Object.defineProperty(this._generators, namespace, {
Expand All @@ -43,10 +47,14 @@ class Store {
});
}

_storeAsModule(namespace, Generator) {
_storeAsModule(namespace, Generator, packageJson) {
this._meta[namespace] = {
resolved: 'unknown',
namespace
namespace,
packageJson,
get version() {
return require(packageJson).version;
}
};

this._generators[namespace] = Generator;
Expand Down

0 comments on commit af55d79

Please sign in to comment.