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 |
|
9 |
|
10 |
|
11 |
|
12 |
|
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 |
|
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 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function 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 |
|
\ | No newline at end of file |