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