wallet-core/node_modules/resolve/lib/node-modules-paths.js

46 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-10-10 03:43:44 +02:00
var path = require('path');
2017-08-14 05:01:11 +02:00
var fs = require('fs');
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)
2017-08-14 05:01:11 +02:00
: ['node_modules'];
2016-10-10 03:43:44 +02:00
// 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
2017-08-14 05:01:11 +02:00
if (opts && opts.preserveSymlinks === false) {
try {
absoluteStart = fs.realpathSync(absoluteStart);
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
}
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;
};