You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

35 lines
1018 B

var path = require('path');
var parse = path.parse || require('path-parse');
module.exports = function nodeModulesPaths(start, opts) {
var modules = opts && opts.moduleDirectory
? [].concat(opts.moduleDirectory)
: ['node_modules']
;
// ensure that `start` is an absolute path at this point,
// resolving against the process' current working directory
start = path.resolve(start);
var prefix = '/';
if (/^([A-Za-z]:)/.test(start)) {
prefix = '';
} else if (/^\\\\/.test(start)) {
prefix = '\\\\';
}
var paths = [start];
var parsed = parse(start);
while (parsed.dir !== paths[paths.length - 1]) {
paths.push(parsed.dir);
parsed = parse(parsed.dir);
}
var dirs = paths.reduce(function (dirs, aPath) {
return dirs.concat(modules.map(function (moduleDir) {
return path.join(prefix, aPath, moduleDir);
}));
}, []);
return opts && opts.paths ? dirs.concat(opts.paths) : dirs;
};