node-modules-paths.js (1107B)
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 9 // ensure that `start` is an absolute path at this point, 10 // resolving against the process' current working directory 11 start = path.resolve(start); 12 13 var prefix = '/'; 14 if (/^([A-Za-z]:)/.test(start)) { 15 prefix = ''; 16 } else if (/^\\\\/.test(start)) { 17 prefix = '\\\\'; 18 } 19 20 var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/; 21 22 var parts = start.split(splitRe); 23 24 var dirs = []; 25 for (var i = parts.length - 1; i >= 0; i--) { 26 if (modules.indexOf(parts[i]) !== -1) continue; 27 dirs = dirs.concat(modules.map(function(module_dir) { 28 return prefix + path.join( 29 path.join.apply(path, parts.slice(0, i + 1)), 30 module_dir 31 ); 32 })); 33 } 34 if (process.platform === 'win32'){ 35 dirs[dirs.length-1] = dirs[dirs.length-1].replace(":", ":\\"); 36 } 37 return dirs.concat(opts.paths); 38 }