Initial commit
[yaffs-website] / node_modules / resolve / lib / node-modules-paths.js
1 var path = require('path');
2 var parse = path.parse || require('path-parse');
3
4 module.exports = function nodeModulesPaths(start, opts) {
5     var modules = opts && opts.moduleDirectory
6         ? [].concat(opts.moduleDirectory)
7         : ['node_modules']
8     ;
9
10     // ensure that `start` is an absolute path at this point,
11     // resolving against the process' current working directory
12     start = path.resolve(start);
13
14     var prefix = '/';
15     if (/^([A-Za-z]:)/.test(start)) {
16         prefix = '';
17     } else if (/^\\\\/.test(start)) {
18         prefix = '\\\\';
19     }
20
21     var paths = [start];
22     var parsed = parse(start);
23     while (parsed.dir !== paths[paths.length - 1]) {
24         paths.push(parsed.dir);
25         parsed = parse(parsed.dir);
26     }
27
28     var dirs = paths.reduce(function (dirs, aPath) {
29         return dirs.concat(modules.map(function (moduleDir) {
30             return path.join(prefix, aPath, moduleDir);
31         }));
32     }, []);
33
34     return opts && opts.paths ? dirs.concat(opts.paths) : dirs;
35 };