1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.matchFromAbsolutePathsAsync = exports.createMatchPathAsync = void 0;
|
4 | var path = require("path");
|
5 | var TryPath = require("./try-path");
|
6 | var MappingEntry = require("./mapping-entry");
|
7 | var Filesystem = require("./filesystem");
|
8 |
|
9 |
|
10 |
|
11 | function createMatchPathAsync(absoluteBaseUrl, paths, mainFields, addMatchAll) {
|
12 | if (mainFields === void 0) { mainFields = ["main"]; }
|
13 | if (addMatchAll === void 0) { addMatchAll = true; }
|
14 | var absolutePaths = MappingEntry.getAbsoluteMappingEntries(absoluteBaseUrl, paths, addMatchAll);
|
15 | return function (requestedModule, readJson, fileExists, extensions, callback) {
|
16 | return matchFromAbsolutePathsAsync(absolutePaths, requestedModule, readJson, fileExists, extensions, callback, mainFields);
|
17 | };
|
18 | }
|
19 | exports.createMatchPathAsync = createMatchPathAsync;
|
20 |
|
21 |
|
22 |
|
23 | function matchFromAbsolutePathsAsync(absolutePathMappings, requestedModule, readJson, fileExists, extensions, callback, mainFields) {
|
24 | if (readJson === void 0) { readJson = Filesystem.readJsonFromDiskAsync; }
|
25 | if (fileExists === void 0) { fileExists = Filesystem.fileExistsAsync; }
|
26 | if (extensions === void 0) { extensions = Object.keys(require.extensions); }
|
27 | if (mainFields === void 0) { mainFields = ["main"]; }
|
28 | var tryPaths = TryPath.getPathsToTry(extensions, absolutePathMappings, requestedModule);
|
29 | if (!tryPaths) {
|
30 | return callback();
|
31 | }
|
32 | findFirstExistingPath(tryPaths, readJson, fileExists, callback, 0, mainFields);
|
33 | }
|
34 | exports.matchFromAbsolutePathsAsync = matchFromAbsolutePathsAsync;
|
35 | function findFirstExistingMainFieldMappedFile(packageJson, mainFields, packageJsonPath, fileExistsAsync, doneCallback, index) {
|
36 | if (index === void 0) { index = 0; }
|
37 | if (index >= mainFields.length) {
|
38 | return doneCallback(undefined, undefined);
|
39 | }
|
40 | var tryNext = function () {
|
41 | return findFirstExistingMainFieldMappedFile(packageJson, mainFields, packageJsonPath, fileExistsAsync, doneCallback, index + 1);
|
42 | };
|
43 | var mainFieldSelector = mainFields[index];
|
44 | var mainFieldMapping = typeof mainFieldSelector === "string"
|
45 | ? packageJson[mainFieldSelector]
|
46 | : mainFieldSelector.reduce(function (obj, key) { return obj[key]; }, packageJson);
|
47 | if (typeof mainFieldMapping !== "string") {
|
48 |
|
49 | return tryNext();
|
50 | }
|
51 | var mappedFilePath = path.join(path.dirname(packageJsonPath), mainFieldMapping);
|
52 | fileExistsAsync(mappedFilePath, function (err, exists) {
|
53 | if (err) {
|
54 | return doneCallback(err);
|
55 | }
|
56 | if (exists) {
|
57 | return doneCallback(undefined, mappedFilePath);
|
58 | }
|
59 | return tryNext();
|
60 | });
|
61 | }
|
62 |
|
63 | function findFirstExistingPath(tryPaths, readJson, fileExists, doneCallback, index, mainFields) {
|
64 | if (index === void 0) { index = 0; }
|
65 | if (mainFields === void 0) { mainFields = ["main"]; }
|
66 | var tryPath = tryPaths[index];
|
67 | if (tryPath.type === "file" ||
|
68 | tryPath.type === "extension" ||
|
69 | tryPath.type === "index") {
|
70 | fileExists(tryPath.path, function (err, exists) {
|
71 | if (err) {
|
72 | return doneCallback(err);
|
73 | }
|
74 | if (exists) {
|
75 | return doneCallback(undefined, TryPath.getStrippedPath(tryPath));
|
76 | }
|
77 | if (index === tryPaths.length - 1) {
|
78 | return doneCallback();
|
79 | }
|
80 |
|
81 | return findFirstExistingPath(tryPaths, readJson, fileExists, doneCallback, index + 1, mainFields);
|
82 | });
|
83 | }
|
84 | else if (tryPath.type === "package") {
|
85 | readJson(tryPath.path, function (err, packageJson) {
|
86 | if (err) {
|
87 | return doneCallback(err);
|
88 | }
|
89 | if (packageJson) {
|
90 | return findFirstExistingMainFieldMappedFile(packageJson, mainFields, tryPath.path, fileExists, function (mainFieldErr, mainFieldMappedFile) {
|
91 | if (mainFieldErr) {
|
92 | return doneCallback(mainFieldErr);
|
93 | }
|
94 | if (mainFieldMappedFile) {
|
95 | return doneCallback(undefined, mainFieldMappedFile);
|
96 | }
|
97 |
|
98 | return findFirstExistingPath(tryPaths, readJson, fileExists, doneCallback, index + 1, mainFields);
|
99 | });
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | return findFirstExistingPath(tryPaths, readJson, fileExists, doneCallback, index + 1, mainFields);
|
110 | });
|
111 | }
|
112 | else {
|
113 | TryPath.exhaustiveTypeException(tryPath.type);
|
114 | }
|
115 | }
|
116 |
|
\ | No newline at end of file |