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