You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.2 KiB

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