| 1 | var path = require('path');
|
|---|
| 2 | var parse = path.parse || require('path-parse'); // eslint-disable-line global-require
|
|---|
| 3 |
|
|---|
| 4 | var driveLetterRegex = /^([A-Za-z]:)/;
|
|---|
| 5 | var uncPathRegex = /^\\\\/;
|
|---|
| 6 |
|
|---|
| 7 | function getNodeModulesDirs(absoluteStart, modules) {
|
|---|
| 8 | var prefix = '/';
|
|---|
| 9 | if (driveLetterRegex.test(absoluteStart)) {
|
|---|
| 10 | prefix = '';
|
|---|
| 11 | } else if (uncPathRegex.test(absoluteStart)) {
|
|---|
| 12 | prefix = '\\\\';
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | var paths = [absoluteStart];
|
|---|
| 16 | var parsed = parse(absoluteStart);
|
|---|
| 17 | while (parsed.dir !== paths[paths.length - 1]) {
|
|---|
| 18 | paths.push(parsed.dir);
|
|---|
| 19 | parsed = parse(parsed.dir);
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | return paths.reduce(function (dirs, aPath) {
|
|---|
| 23 | return dirs.concat(modules.map(function (moduleDir) {
|
|---|
| 24 | return path.resolve(prefix, aPath, moduleDir);
|
|---|
| 25 | }));
|
|---|
| 26 | }, []);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | module.exports = function nodeModulesPaths(start, opts, request) {
|
|---|
| 30 | var modules = opts && opts.moduleDirectory
|
|---|
| 31 | ? [].concat(opts.moduleDirectory)
|
|---|
| 32 | : ['node_modules'];
|
|---|
| 33 |
|
|---|
| 34 | if (opts && typeof opts.paths === 'function') {
|
|---|
| 35 | return opts.paths(
|
|---|
| 36 | request,
|
|---|
| 37 | start,
|
|---|
| 38 | function () { return getNodeModulesDirs(start, modules); },
|
|---|
| 39 | opts
|
|---|
| 40 | );
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | var dirs = getNodeModulesDirs(start, modules);
|
|---|
| 44 | return opts && opts.paths ? dirs.concat(opts.paths) : dirs;
|
|---|
| 45 | };
|
|---|