1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var fastPath = _interopRequireWildcard(require('./lib/fast_path'));
|
9 |
|
10 | var _constants = _interopRequireDefault(require('./constants'));
|
11 |
|
12 | function _interopRequireDefault(obj) {
|
13 | return obj && obj.__esModule ? obj : {default: obj};
|
14 | }
|
15 |
|
16 | function _getRequireWildcardCache() {
|
17 | if (typeof WeakMap !== 'function') return null;
|
18 | var cache = new WeakMap();
|
19 | _getRequireWildcardCache = function () {
|
20 | return cache;
|
21 | };
|
22 | return cache;
|
23 | }
|
24 |
|
25 | function _interopRequireWildcard(obj) {
|
26 | if (obj && obj.__esModule) {
|
27 | return obj;
|
28 | }
|
29 | if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
|
30 | return {default: obj};
|
31 | }
|
32 | var cache = _getRequireWildcardCache();
|
33 | if (cache && cache.has(obj)) {
|
34 | return cache.get(obj);
|
35 | }
|
36 | var newObj = {};
|
37 | var hasPropertyDescriptor =
|
38 | Object.defineProperty && Object.getOwnPropertyDescriptor;
|
39 | for (var key in obj) {
|
40 | if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
41 | var desc = hasPropertyDescriptor
|
42 | ? Object.getOwnPropertyDescriptor(obj, key)
|
43 | : null;
|
44 | if (desc && (desc.get || desc.set)) {
|
45 | Object.defineProperty(newObj, key, desc);
|
46 | } else {
|
47 | newObj[key] = obj[key];
|
48 | }
|
49 | }
|
50 | }
|
51 | newObj.default = obj;
|
52 | if (cache) {
|
53 | cache.set(obj, newObj);
|
54 | }
|
55 | return newObj;
|
56 | }
|
57 |
|
58 | function _defineProperty(obj, key, value) {
|
59 | if (key in obj) {
|
60 | Object.defineProperty(obj, key, {
|
61 | value: value,
|
62 | enumerable: true,
|
63 | configurable: true,
|
64 | writable: true
|
65 | });
|
66 | } else {
|
67 | obj[key] = value;
|
68 | }
|
69 | return obj;
|
70 | }
|
71 |
|
72 | const EMPTY_OBJ = {};
|
73 | const EMPTY_MAP = new Map();
|
74 |
|
75 | class ModuleMap {
|
76 | static mapToArrayRecursive(map) {
|
77 | let arr = Array.from(map);
|
78 |
|
79 | if (arr[0] && arr[0][1] instanceof Map) {
|
80 | arr = arr.map(el => [el[0], this.mapToArrayRecursive(el[1])]);
|
81 | }
|
82 |
|
83 | return arr;
|
84 | }
|
85 |
|
86 | static mapFromArrayRecursive(arr) {
|
87 | if (arr[0] && Array.isArray(arr[1])) {
|
88 | arr = arr.map(el => [el[0], this.mapFromArrayRecursive(el[1])]);
|
89 | }
|
90 |
|
91 | return new Map(arr);
|
92 | }
|
93 |
|
94 | constructor(raw) {
|
95 | _defineProperty(this, '_raw', void 0);
|
96 |
|
97 | _defineProperty(this, 'json', void 0);
|
98 |
|
99 | this._raw = raw;
|
100 | }
|
101 |
|
102 | getModule(name, platform, supportsNativePlatform, type) {
|
103 | if (type == null) {
|
104 | type = _constants.default.MODULE;
|
105 | }
|
106 |
|
107 | const module = this._getModuleMetadata(
|
108 | name,
|
109 | platform,
|
110 | !!supportsNativePlatform
|
111 | );
|
112 |
|
113 | if (module && module[_constants.default.TYPE] === type) {
|
114 | const modulePath = module[_constants.default.PATH];
|
115 | return modulePath && fastPath.resolve(this._raw.rootDir, modulePath);
|
116 | }
|
117 |
|
118 | return null;
|
119 | }
|
120 |
|
121 | getPackage(name, platform, _supportsNativePlatform) {
|
122 | return this.getModule(name, platform, null, _constants.default.PACKAGE);
|
123 | }
|
124 |
|
125 | getMockModule(name) {
|
126 | const mockPath =
|
127 | this._raw.mocks.get(name) || this._raw.mocks.get(name + '/index');
|
128 |
|
129 | return mockPath && fastPath.resolve(this._raw.rootDir, mockPath);
|
130 | }
|
131 |
|
132 | getRawModuleMap() {
|
133 | return {
|
134 | duplicates: this._raw.duplicates,
|
135 | map: this._raw.map,
|
136 | mocks: this._raw.mocks,
|
137 | rootDir: this._raw.rootDir
|
138 | };
|
139 | }
|
140 |
|
141 | toJSON() {
|
142 | if (!this.json) {
|
143 | this.json = {
|
144 | duplicates: ModuleMap.mapToArrayRecursive(this._raw.duplicates),
|
145 | map: Array.from(this._raw.map),
|
146 | mocks: Array.from(this._raw.mocks),
|
147 | rootDir: this._raw.rootDir
|
148 | };
|
149 | }
|
150 |
|
151 | return this.json;
|
152 | }
|
153 |
|
154 | static fromJSON(serializableModuleMap) {
|
155 | return new ModuleMap({
|
156 | duplicates: ModuleMap.mapFromArrayRecursive(
|
157 | serializableModuleMap.duplicates
|
158 | ),
|
159 | map: new Map(serializableModuleMap.map),
|
160 | mocks: new Map(serializableModuleMap.mocks),
|
161 | rootDir: serializableModuleMap.rootDir
|
162 | });
|
163 | }
|
164 | |
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | _getModuleMetadata(name, platform, supportsNativePlatform) {
|
174 | const map = this._raw.map.get(name) || EMPTY_OBJ;
|
175 | const dupMap = this._raw.duplicates.get(name) || EMPTY_MAP;
|
176 |
|
177 | if (platform != null) {
|
178 | this._assertNoDuplicates(
|
179 | name,
|
180 | platform,
|
181 | supportsNativePlatform,
|
182 | dupMap.get(platform)
|
183 | );
|
184 |
|
185 | if (map[platform] != null) {
|
186 | return map[platform];
|
187 | }
|
188 | }
|
189 |
|
190 | if (supportsNativePlatform) {
|
191 | this._assertNoDuplicates(
|
192 | name,
|
193 | _constants.default.NATIVE_PLATFORM,
|
194 | supportsNativePlatform,
|
195 | dupMap.get(_constants.default.NATIVE_PLATFORM)
|
196 | );
|
197 |
|
198 | if (map[_constants.default.NATIVE_PLATFORM]) {
|
199 | return map[_constants.default.NATIVE_PLATFORM];
|
200 | }
|
201 | }
|
202 |
|
203 | this._assertNoDuplicates(
|
204 | name,
|
205 | _constants.default.GENERIC_PLATFORM,
|
206 | supportsNativePlatform,
|
207 | dupMap.get(_constants.default.GENERIC_PLATFORM)
|
208 | );
|
209 |
|
210 | if (map[_constants.default.GENERIC_PLATFORM]) {
|
211 | return map[_constants.default.GENERIC_PLATFORM];
|
212 | }
|
213 |
|
214 | return null;
|
215 | }
|
216 |
|
217 | _assertNoDuplicates(name, platform, supportsNativePlatform, relativePathSet) {
|
218 | if (relativePathSet == null) {
|
219 | return;
|
220 | }
|
221 |
|
222 | const previousSet = relativePathSet;
|
223 | const duplicates = new Map();
|
224 |
|
225 | for (const [relativePath, type] of previousSet) {
|
226 | const duplicatePath = fastPath.resolve(this._raw.rootDir, relativePath);
|
227 | duplicates.set(duplicatePath, type);
|
228 | }
|
229 |
|
230 | throw new DuplicateHasteCandidatesError(
|
231 | name,
|
232 | platform,
|
233 | supportsNativePlatform,
|
234 | duplicates
|
235 | );
|
236 | }
|
237 |
|
238 | static create(rootDir) {
|
239 | return new ModuleMap({
|
240 | duplicates: new Map(),
|
241 | map: new Map(),
|
242 | mocks: new Map(),
|
243 | rootDir
|
244 | });
|
245 | }
|
246 | }
|
247 |
|
248 | exports.default = ModuleMap;
|
249 |
|
250 | _defineProperty(ModuleMap, 'DuplicateHasteCandidatesError', void 0);
|
251 |
|
252 | class DuplicateHasteCandidatesError extends Error {
|
253 | constructor(name, platform, supportsNativePlatform, duplicatesSet) {
|
254 | const platformMessage = getPlatformMessage(platform);
|
255 | super(
|
256 | `The name \`${name}\` was looked up in the Haste module map. It ` +
|
257 | `cannot be resolved, because there exists several different ` +
|
258 | `files, or packages, that provide a module for ` +
|
259 | `that particular name and platform. ${platformMessage} You must ` +
|
260 | `delete or exclude files until there remains only one of these:\n\n` +
|
261 | Array.from(duplicatesSet)
|
262 | .map(
|
263 | ([dupFilePath, dupFileType]) =>
|
264 | ` * \`${dupFilePath}\` (${getTypeMessage(dupFileType)})\n`
|
265 | )
|
266 | .sort()
|
267 | .join('')
|
268 | );
|
269 |
|
270 | _defineProperty(this, 'hasteName', void 0);
|
271 |
|
272 | _defineProperty(this, 'platform', void 0);
|
273 |
|
274 | _defineProperty(this, 'supportsNativePlatform', void 0);
|
275 |
|
276 | _defineProperty(this, 'duplicatesSet', void 0);
|
277 |
|
278 | this.hasteName = name;
|
279 | this.platform = platform;
|
280 | this.supportsNativePlatform = supportsNativePlatform;
|
281 | this.duplicatesSet = duplicatesSet;
|
282 | }
|
283 | }
|
284 |
|
285 | function getPlatformMessage(platform) {
|
286 | if (platform === _constants.default.GENERIC_PLATFORM) {
|
287 | return 'The platform is generic (no extension).';
|
288 | }
|
289 |
|
290 | return `The platform extension is \`${platform}\`.`;
|
291 | }
|
292 |
|
293 | function getTypeMessage(type) {
|
294 | switch (type) {
|
295 | case _constants.default.MODULE:
|
296 | return 'module';
|
297 |
|
298 | case _constants.default.PACKAGE:
|
299 | return 'package';
|
300 | }
|
301 |
|
302 | return 'unknown';
|
303 | }
|
304 |
|
305 | ModuleMap.DuplicateHasteCandidatesError = DuplicateHasteCandidatesError;
|