7c58e1062a4126c93693c8cc8face47f374228ec
[platform/framework/web/crosswalk-tizen.git] /
1 var path = require('path');
2
3 module.exports = function (start, opts) {
4     var modules = opts.moduleDirectory
5         ? [].concat(opts.moduleDirectory)
6         : ['node_modules']
7     ;
8     var prefix = '/';
9     if (/^([A-Za-z]:)/.test(start)) {
10         prefix = '';
11     } else if (/^\\\\/.test(start)) {
12         prefix = '\\\\';
13     }
14     var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/;
15
16     // ensure that `start` is an absolute path at this point,
17     // resolving againt the process' current working directory
18     start = path.resolve(start);
19
20     var parts = start.split(splitRe);
21
22     var dirs = [];
23     for (var i = parts.length - 1; i >= 0; i--) {
24         if (modules.indexOf(parts[i]) !== -1) continue;
25         dirs = dirs.concat(modules.map(function(module_dir) {
26             return prefix + path.join(
27                 path.join.apply(path, parts.slice(0, i + 1)),
28                 module_dir
29             );
30         }));
31     }
32     if (process.platform === 'win32'){
33         dirs[dirs.length-1] = dirs[dirs.length-1].replace(":", ":\\");
34     }
35     return dirs.concat(opts.paths);
36 }