Skip to content

Commit

Permalink
lookupNamespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jan 24, 2020
1 parent 3956407 commit 8af04cd
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions lib/resolver.js
Expand Up @@ -84,29 +84,35 @@ resolver.lookup = function (options = {localOnly: false}, cb) {
return generators;
};

resolver.lookupNamespaces = function (namespaces, options) {
/**
* Search for generators or sub generators by namespace.
*
* @param {boolean|Object} [options] options passed to lookup. Options singleResult,
* filePatterns and packagePatterns can be overridden
* @return {Array|Object} List of generators
*/
resolver.lookupNamespaces = function (namespaces, options = {}) {
namespaces = Array.isArray(namespaces) ? namespaces : [namespaces];
const parsed = namespaces.map(ns => requireNamespace(ns));
const opts = parsed.map(ns => {
let filePatterns;
const nsOpts = {packagePatterns: ns.generatorHint};
if (ns.generator) {
// Build filePatterns to look specifically for the namespace.
const genPath = ns.generator.split(':').join('/');
filePatterns = [`${genPath}/index.js`, `${genPath}.js`];
filePatterns = this.lookups.map(prefix => {
let filePatterns = [`${genPath}/index.js`, `${genPath}.js`];
const lookups = options.lookups || this.lookups;
filePatterns = lookups.map(prefix => {
return filePatterns.map(pattern => path.join(prefix, pattern));
}).reduce(
(accumulator, currentValue) => accumulator.concat(currentValue),
[]
);
nsOpts.filePatterns = filePatterns;
nsOpts.singleResult = true;
}
return {packagePatterns: ns.generatorHint, filePatterns};
return nsOpts;
});
return opts
.map(opt => this.lookup({singleResult: true, ...options, ...opt}))
.reduce(
(accumulator, currentValue) => accumulator.concat(currentValue),
[]
);
return opts.map(opt => this.lookup({...options, ...opt}));
};

/**
Expand Down

0 comments on commit 8af04cd

Please sign in to comment.