UNPKG

3.77 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.exhaustiveTypeException = exports.getStrippedPath = exports.getPathsToTry = void 0;
4var path = require("path");
5var path_1 = require("path");
6var 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 */
14function 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}
44exports.getPathsToTry = getPathsToTry;
45// Not sure why we don't just return the full found path?
46function 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}
57exports.getStrippedPath = getStrippedPath;
58function exhaustiveTypeException(check) {
59 throw new Error("Unknown type ".concat(check));
60}
61exports.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 *
66 * @param patttern for example "foo*"
67 * @param search for example "fooawesomebar"
68 * @returns the part of search that * matches, or undefined if no match.
69 */
70function matchStar(pattern, search) {
71 if (search.length < pattern.length) {
72 return undefined;
73 }
74 if (pattern === "*") {
75 return search;
76 }
77 var star = pattern.indexOf("*");
78 if (star === -1) {
79 return undefined;
80 }
81 var part1 = pattern.substring(0, star);
82 var part2 = pattern.substring(star + 1);
83 if (search.substr(0, star) !== part1) {
84 return undefined;
85 }
86 if (search.substr(search.length - part2.length) !== part2) {
87 return undefined;
88 }
89 return search.substr(star, search.length - part2.length);
90}
91//# sourceMappingURL=try-path.js.map
\No newline at end of file