Resolves local and global npm modules that match specified patterns, and returns a configuration object for each resolved module.
Install with npm:
$ npm install resolve-modules --save
var Resolver = require('resolve-modules');
var resolver = new Resolver();
Iterates over npm-paths and emits file
for every resolved filepath, and match
for files that match any specified matchers. Paths are cached in memory using a few different objects:
cache.paths
: array of absolute directory and file pathscache.names
: object of vinyl files, wherefile.name
is the object key.file.name
is the basename of the filepath, but it's aliased asname
so we can use it without touching the getters/setters on the vinyl file.cache.files
: array of vinyl files
Params
options
{Object}: Specify a cache to use onoptions.cache
.
Example
var resolver = new Resolver();
resolver.resolve();
console.log(resolver);
Iterates over npm-paths and returns an array of vinyl files that match any provided matchers. Also emits file
for all files, and match
for matches. Additionally, paths are cached on the first call to .resolve
so that any subsequent calls during the same process will use the cached filepaths instead of hitting the file system again. You can force .resolve
to hit the file system again by deleting or nulling resolver.cache.dirs
.
Params
fn
{Function|String|Array|RegExp}: Optionally specify a matcher value.options
{Object}returns
{Array}: Returns an array of vinyl files.
Example
resolver.match('verb', function(basename, file) {
return basename === 'verb';
});
// matches
console.log(resolver.resolve());
// all cached paths
console.log(resolver);
Find a filepath where file.basename
exactly matches the given name
. This method is standalone and does not require use of the .resolve
method or matchers.
Params
name
{String}: Basename of the file to match.returns
{String|undefined}: Returns the absolute filepath if a match is found, otherwise undefined.
Example
var filepath = resolver.find('foo');
Define a matcher to use for matching files when the resolve
method is called. If a string or array of strings is passed, strict equality is used for comparisons with file.name
.
Params
name
{String|Function|Array|RegExp}: Optionally providename
to emit when a match is found, a matcher function, string to match, array of strings, or regex.val
{String|Function|Array|RegExp}: Matcher function, string to match, array of strings, or regex.options
{Object}: If a string is passed, options may be passed to micromatch to convert the string to regex.returns
{Object}: Returns the instance for chaining.
Example
resolver.match('foo');
Define a filter function, glob, string or regex to use for excluding files before matchers are run.
Params
val
{String|RegExp|Function}options
{Object}returns
{Object}
Example
resolver.filter('*.foo');
Define a matcher to use for matching files when the resolve
method is called. If a string or array of strings is passed, any file.name
that contains the given string or strings will return true.
Params
name
{String|Function|Array|RegExp}: Optionally providename
to emit when a match is found, a matcher function, string to match, array of strings, or regex.val
{String|Function|Array|RegExp}: Matcher function, string to match, array of strings, or regex.options
{Object}: If a string is passed, options may be passed to micromatch to convert the string to regex.returns
{Object}: Returns the instance for chaining.
Example
resolver.contains('foo');
Resolve sub-directories from npm-paths. This method probably doesn't need to be used directly, but it's exposed in case you want to customize behavior. Also note that options.recurse
must be defined as true
to recurse into child directories. Alternative, if any matcher is a glob pattern with a globstar (double star: **
), options.recurse
will automatically be set to true
.
Params
fn
{Function}: Optionally specify a filter function to use on filepaths. If provided, the function will be called before any matchers are called.basename
andfile
are exposed to the filter function as arguments, wherefile
is an instance of vinyl.returns
{Object}: Returns the cache.
Events
emits
:ignore
when a file is removed.
Example
resolver.resolveDirs(function(basename, file) {
return !/foo/.test(file.path);
});
You might also be interested in these projects:
- global-modules: The directory used by npm for globally installed npm modules. | homepage
- global-paths: Returns an array of unique "global" directories based on the user's platform and environment. The… more | homepage
- global-prefix: Get the npm global path prefix. | homepage
- npm-paths: Returns an array of unique "npm" directories based on the user's platform and environment. | homepage
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Generate readme and API documentation with verb:
$ npm install verb && npm run docs
Or, if verb is installed globally:
$ verb
Install dev dependencies:
$ npm install -d && npm test
Jon Schlinkert
Copyright © 2016, Jon Schlinkert. Released under the MIT license.
This file was generated by verb, v0.9.0, on May 19, 2016.