node-modules-paths.js 1.1 KB

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