try-path.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.exhaustiveTypeException = exports.getStrippedPath = exports.getPathsToTry = void 0;
  4. var path = require("path");
  5. var path_1 = require("path");
  6. var filesystem_1 = require("./filesystem");
  7. /**
  8. * Builds a list of all physical paths to try by:
  9. * 1. Check for file named exactly as request.
  10. * 2. Check for files named as request ending in any of the extensions.
  11. * 3. Check for file specified in package.json's main property.
  12. * 4. Check for files named as request ending in "index" with any of the extensions.
  13. */
  14. function getPathsToTry(extensions, absolutePathMappings, requestedModule) {
  15. if (!absolutePathMappings || !requestedModule || requestedModule[0] === ".") {
  16. return undefined;
  17. }
  18. var pathsToTry = [];
  19. for (var _i = 0, absolutePathMappings_1 = absolutePathMappings; _i < absolutePathMappings_1.length; _i++) {
  20. var entry = absolutePathMappings_1[_i];
  21. var starMatch = entry.pattern === requestedModule
  22. ? ""
  23. : matchStar(entry.pattern, requestedModule);
  24. if (starMatch !== undefined) {
  25. var _loop_1 = function (physicalPathPattern) {
  26. var physicalPath = physicalPathPattern.replace("*", starMatch);
  27. pathsToTry.push({ type: "file", path: physicalPath });
  28. pathsToTry.push.apply(pathsToTry, extensions.map(function (e) { return ({ type: "extension", path: physicalPath + e }); }));
  29. pathsToTry.push({
  30. type: "package",
  31. path: path.join(physicalPath, "/package.json"),
  32. });
  33. var indexPath = path.join(physicalPath, "/index");
  34. pathsToTry.push.apply(pathsToTry, extensions.map(function (e) { return ({ type: "index", path: indexPath + e }); }));
  35. };
  36. for (var _a = 0, _b = entry.paths; _a < _b.length; _a++) {
  37. var physicalPathPattern = _b[_a];
  38. _loop_1(physicalPathPattern);
  39. }
  40. }
  41. }
  42. return pathsToTry.length === 0 ? undefined : pathsToTry;
  43. }
  44. exports.getPathsToTry = getPathsToTry;
  45. // Not sure why we don't just return the full found path?
  46. function getStrippedPath(tryPath) {
  47. return tryPath.type === "index"
  48. ? (0, path_1.dirname)(tryPath.path)
  49. : tryPath.type === "file"
  50. ? tryPath.path
  51. : tryPath.type === "extension"
  52. ? (0, filesystem_1.removeExtension)(tryPath.path)
  53. : tryPath.type === "package"
  54. ? tryPath.path
  55. : exhaustiveTypeException(tryPath.type);
  56. }
  57. exports.getStrippedPath = getStrippedPath;
  58. function exhaustiveTypeException(check) {
  59. throw new Error("Unknown type ".concat(check));
  60. }
  61. exports.exhaustiveTypeException = exhaustiveTypeException;
  62. /**
  63. * Matches pattern with a single star against search.
  64. * Star must match at least one character to be considered a match.
  65. * @param patttern for example "foo*"
  66. * @param search for example "fooawesomebar"
  67. * @returns the part of search that * matches, or undefined if no match.
  68. */
  69. function matchStar(pattern, search) {
  70. if (search.length < pattern.length) {
  71. return undefined;
  72. }
  73. if (pattern === "*") {
  74. return search;
  75. }
  76. var star = pattern.indexOf("*");
  77. if (star === -1) {
  78. return undefined;
  79. }
  80. var part1 = pattern.substring(0, star);
  81. var part2 = pattern.substring(star + 1);
  82. if (search.substr(0, star) !== part1) {
  83. return undefined;
  84. }
  85. if (search.substr(search.length - part2.length) !== part2) {
  86. return undefined;
  87. }
  88. return search.substr(star, search.length - part2.length);
  89. }
  90. //# sourceMappingURL=try-path.js.map