Skip to content

Resolves local and global npm modules that match specified patterns, and returns a configuration object for each resolved module.

License

Notifications You must be signed in to change notification settings

jonschlinkert/resolve-modules

Repository files navigation

resolve-modules NPM version NPM downloads Build Status

Resolves local and global npm modules that match specified patterns, and returns a configuration object for each resolved module.

Install

Install with npm:

$ npm install resolve-modules --save

Usage

var Resolver = require('resolve-modules');
var resolver = new Resolver();

API

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 paths
  • cache.names: object of vinyl files, where file.name is the object key. file.name is the basename of the filepath, but it's aliased as name 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 on options.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 provide name 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 provide name 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 and file are exposed to the filter function as arguments, where file 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);
});

Related projects

You might also be interested in these projects:

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Building docs

Generate readme and API documentation with verb:

$ npm install verb && npm run docs

Or, if verb is installed globally:

$ verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Jon Schlinkert

License

Copyright © 2016, Jon Schlinkert. Released under the MIT license.


This file was generated by verb, v0.9.0, on May 19, 2016.

About

Resolves local and global npm modules that match specified patterns, and returns a configuration object for each resolved module.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published