try-path.js 3.6 KB

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