Skip to content

Commit

Permalink
Rename multiple to singleResult and improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jan 10, 2020
1 parent ee8d3c6 commit a06b0fc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/environment.js
Expand Up @@ -151,10 +151,12 @@ class Environment extends EventEmitter {
* globally-installed generators.
* @param {Boolean|Object} [options.packagePath=false] - Set true to return the package
* path instead of generators file.
* @param {boolean} [options.singleResult=true] - Set false to return all occurrences.
* @return {String} generator
*/
static lookupGenerator(namespaces, options = {}) {
options = {multiple: false, ...options, skipRegister: true, namespaces};
// Multiple was renamed to !singleResult
options = {singleResult: !options.multiple, ...options, skipRegister: true, namespaces, packageNamespace: options.packagePath};
debug('Performing lookupGenerator with %o', options);

const modules = resolver.lookup(options);
Expand All @@ -163,7 +165,7 @@ class Environment extends EventEmitter {
}

const getPath = module => options.packagePath ? module.packagePath : options.generatorPath ? module.generatorPath : module.generator;
if (!options.multiple) {
if (options.singleResult) {
return getPath(modules[0]);
}
return modules.map(getPath);
Expand Down
15 changes: 11 additions & 4 deletions lib/resolver.js
Expand Up @@ -40,8 +40,16 @@ Object.assign(resolver, {
* @param {boolean|Object} [options]
* @param {boolean} [options.localOnly = false] - Set true to skip lookups of
* globally-installed generators.
* @param {boolean} [options.npmPaths] - Paths to look generators at.
* @param {boolean} [options.singlePackage=false] - Don't look for generators at npmPaths childs.
* @param {boolean} [options.filterPaths = false] - Ignore paths that aren't a known repository
* (don't touch at NODE_PATH paths).
* @param {string|Array} [options.npmPaths] - Paths to look generators at.
* @param {string|Array} [options.namespaces] - Namespaces to look for.
* @param {string|Array} [options.pattern] - Alternative way to narrow down packages to look for. Ex 'generator-foo-*'.
* @param {boolean} [options.singlePackage=false] - If true don't consider npmPaths repository paths,
* It won't look for generators at npmPaths childs.
* @param {boolean} [options.skipRegister=false] - Don't try to register the generator into environment.
* @param {boolean} [options.singleResult=false] - Set true to return the first match ocurrence.
* @param {boolean} [options.packageNamespace=false] - Set true to do a package namespace lookup.
* @param {function} cb - Callback called once the lookup is done. Take err as first
* parameter.
*/
Expand All @@ -55,7 +63,6 @@ resolver.lookup = function (options = {localOnly: false}, cb) {
options = {localOnly: options};
}

options = {multiple: true, ...options};
if (options.namespaces) {
options.namespaces = Array.isArray(options.namespaces) ? options.namespaces : [options.namespaces];
options.pattern = options.pattern || options.namespaces.map(ns => requireNamespace(ns).generatorHint);
Expand Down Expand Up @@ -84,7 +91,7 @@ resolver.lookup = function (options = {localOnly: false}, cb) {
if (!options.skipRegister) {
this._tryRegistering(filename, modulePath);
}
if (!options.multiple) {
if (options.singleResult) {
return modules;
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/resolver.js
Expand Up @@ -461,6 +461,10 @@ describe('Environment Resolver', function () {
const multiplePath = Environment.lookupGenerator('module:app', {multiple: true});
assert.ok(multiplePath[0].endsWith('lookup-project/node_modules/generator-module/generators/app/index.js'));
assert.ok(multiplePath[1].endsWith('lookup-project/node_modules/foo/node_modules/generator-module/generators/app/index.js'));

const multiplePath2 = Environment.lookupGenerator('module:app', {singleResult: false});
assert.ok(multiplePath2[0].endsWith('lookup-project/node_modules/generator-module/generators/app/index.js'));
assert.ok(multiplePath2[1].endsWith('lookup-project/node_modules/foo/node_modules/generator-module/generators/app/index.js'));
});
});
});
Expand Down

0 comments on commit a06b0fc

Please sign in to comment.