1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getIdentVendorPath = exports.prettyDependent = exports.prettyResolution = exports.prettyWorkspace = exports.sortDescriptors = exports.prettyLocatorNoColors = exports.prettyLocator = exports.prettyReference = exports.prettyDescriptor = exports.prettyRange = exports.prettyIdent = exports.slugifyLocator = exports.slugifyIdent = exports.stringifyLocator = exports.stringifyDescriptor = exports.stringifyIdent = exports.requirableIdent = exports.convertToManifestRange = exports.makeRange = exports.parseFileStyleRange = exports.parseRange = exports.tryParseLocator = exports.parseLocator = exports.tryParseDescriptor = exports.parseDescriptor = exports.tryParseIdent = exports.parseIdent = exports.areVirtualPackagesEquivalent = exports.areLocatorsEqual = exports.areDescriptorsEqual = exports.areIdentsEqual = exports.bindLocator = exports.bindDescriptor = exports.devirtualizeLocator = exports.devirtualizeDescriptor = exports.isVirtualLocator = exports.isVirtualDescriptor = exports.virtualizePackage = exports.virtualizeDescriptor = exports.copyPackage = exports.renamePackage = exports.convertPackageToLocator = exports.convertLocatorToDescriptor = exports.convertDescriptorToLocator = exports.convertToIdent = exports.makeLocator = exports.makeDescriptor = exports.makeIdent = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const fslib_1 = require("@yarnpkg/fslib");
|
6 | const querystring_1 = tslib_1.__importDefault(require("querystring"));
|
7 | const semver_1 = tslib_1.__importDefault(require("semver"));
|
8 | const formatUtils = tslib_1.__importStar(require("./formatUtils"));
|
9 | const hashUtils = tslib_1.__importStar(require("./hashUtils"));
|
10 | const miscUtils = tslib_1.__importStar(require("./miscUtils"));
|
11 | const structUtils = tslib_1.__importStar(require("./structUtils"));
|
12 | const VIRTUAL_PROTOCOL = `virtual:`;
|
13 | const VIRTUAL_ABBREVIATE = 5;
|
14 | function makeIdent(scope, name) {
|
15 | if (scope === null || scope === void 0 ? void 0 : scope.startsWith(`@`))
|
16 | throw new Error(`Invalid scope: don't prefix it with '@'`);
|
17 | return { identHash: hashUtils.makeHash(scope, name), scope, name };
|
18 | }
|
19 | exports.makeIdent = makeIdent;
|
20 | function makeDescriptor(ident, range) {
|
21 | return { identHash: ident.identHash, scope: ident.scope, name: ident.name, descriptorHash: hashUtils.makeHash(ident.identHash, range), range };
|
22 | }
|
23 | exports.makeDescriptor = makeDescriptor;
|
24 | function makeLocator(ident, reference) {
|
25 | return { identHash: ident.identHash, scope: ident.scope, name: ident.name, locatorHash: hashUtils.makeHash(ident.identHash, reference), reference };
|
26 | }
|
27 | exports.makeLocator = makeLocator;
|
28 | function convertToIdent(source) {
|
29 | return { identHash: source.identHash, scope: source.scope, name: source.name };
|
30 | }
|
31 | exports.convertToIdent = convertToIdent;
|
32 | function convertDescriptorToLocator(descriptor) {
|
33 | return { identHash: descriptor.identHash, scope: descriptor.scope, name: descriptor.name, locatorHash: descriptor.descriptorHash, reference: descriptor.range };
|
34 | }
|
35 | exports.convertDescriptorToLocator = convertDescriptorToLocator;
|
36 | function convertLocatorToDescriptor(locator) {
|
37 | return { identHash: locator.identHash, scope: locator.scope, name: locator.name, descriptorHash: locator.locatorHash, range: locator.reference };
|
38 | }
|
39 | exports.convertLocatorToDescriptor = convertLocatorToDescriptor;
|
40 | function convertPackageToLocator(pkg) {
|
41 | return { identHash: pkg.identHash, scope: pkg.scope, name: pkg.name, locatorHash: pkg.locatorHash, reference: pkg.reference };
|
42 | }
|
43 | exports.convertPackageToLocator = convertPackageToLocator;
|
44 | function renamePackage(pkg, locator) {
|
45 | return {
|
46 | identHash: locator.identHash,
|
47 | scope: locator.scope,
|
48 | name: locator.name,
|
49 | locatorHash: locator.locatorHash,
|
50 | reference: locator.reference,
|
51 | version: pkg.version,
|
52 | languageName: pkg.languageName,
|
53 | linkType: pkg.linkType,
|
54 | dependencies: new Map(pkg.dependencies),
|
55 | peerDependencies: new Map(pkg.peerDependencies),
|
56 | dependenciesMeta: new Map(pkg.dependenciesMeta),
|
57 | peerDependenciesMeta: new Map(pkg.peerDependenciesMeta),
|
58 | bin: new Map(pkg.bin),
|
59 | };
|
60 | }
|
61 | exports.renamePackage = renamePackage;
|
62 | function copyPackage(pkg) {
|
63 | return renamePackage(pkg, pkg);
|
64 | }
|
65 | exports.copyPackage = copyPackage;
|
66 | function virtualizeDescriptor(descriptor, entropy) {
|
67 | if (entropy.includes(`#`))
|
68 | throw new Error(`Invalid entropy`);
|
69 | return makeDescriptor(descriptor, `virtual:${entropy}#${descriptor.range}`);
|
70 | }
|
71 | exports.virtualizeDescriptor = virtualizeDescriptor;
|
72 | function virtualizePackage(pkg, entropy) {
|
73 | if (entropy.includes(`#`))
|
74 | throw new Error(`Invalid entropy`);
|
75 | return renamePackage(pkg, makeLocator(pkg, `virtual:${entropy}#${pkg.reference}`));
|
76 | }
|
77 | exports.virtualizePackage = virtualizePackage;
|
78 | function isVirtualDescriptor(descriptor) {
|
79 | return descriptor.range.startsWith(VIRTUAL_PROTOCOL);
|
80 | }
|
81 | exports.isVirtualDescriptor = isVirtualDescriptor;
|
82 | function isVirtualLocator(locator) {
|
83 | return locator.reference.startsWith(VIRTUAL_PROTOCOL);
|
84 | }
|
85 | exports.isVirtualLocator = isVirtualLocator;
|
86 | function devirtualizeDescriptor(descriptor) {
|
87 | if (!isVirtualDescriptor(descriptor))
|
88 | throw new Error(`Not a virtual descriptor`);
|
89 | return makeDescriptor(descriptor, descriptor.range.replace(/^[^#]*#/, ``));
|
90 | }
|
91 | exports.devirtualizeDescriptor = devirtualizeDescriptor;
|
92 | function devirtualizeLocator(locator) {
|
93 | if (!isVirtualLocator(locator))
|
94 | throw new Error(`Not a virtual descriptor`);
|
95 | return makeLocator(locator, locator.reference.replace(/^[^#]*#/, ``));
|
96 | }
|
97 | exports.devirtualizeLocator = devirtualizeLocator;
|
98 | function bindDescriptor(descriptor, params) {
|
99 | if (descriptor.range.includes(`::`))
|
100 | return descriptor;
|
101 | return makeDescriptor(descriptor, `${descriptor.range}::${querystring_1.default.stringify(params)}`);
|
102 | }
|
103 | exports.bindDescriptor = bindDescriptor;
|
104 | function bindLocator(locator, params) {
|
105 | if (locator.reference.includes(`::`))
|
106 | return locator;
|
107 | return makeLocator(locator, `${locator.reference}::${querystring_1.default.stringify(params)}`);
|
108 | }
|
109 | exports.bindLocator = bindLocator;
|
110 | function areIdentsEqual(a, b) {
|
111 | return a.identHash === b.identHash;
|
112 | }
|
113 | exports.areIdentsEqual = areIdentsEqual;
|
114 | function areDescriptorsEqual(a, b) {
|
115 | return a.descriptorHash === b.descriptorHash;
|
116 | }
|
117 | exports.areDescriptorsEqual = areDescriptorsEqual;
|
118 | function areLocatorsEqual(a, b) {
|
119 | return a.locatorHash === b.locatorHash;
|
120 | }
|
121 | exports.areLocatorsEqual = areLocatorsEqual;
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function areVirtualPackagesEquivalent(a, b) {
|
128 | if (!isVirtualLocator(a))
|
129 | throw new Error(`Invalid package type`);
|
130 | if (!isVirtualLocator(b))
|
131 | throw new Error(`Invalid package type`);
|
132 | if (!areIdentsEqual(a, b))
|
133 | return false;
|
134 | if (a.dependencies.size !== b.dependencies.size)
|
135 | return false;
|
136 | for (const dependencyDescriptorA of a.dependencies.values()) {
|
137 | const dependencyDescriptorB = b.dependencies.get(dependencyDescriptorA.identHash);
|
138 | if (!dependencyDescriptorB)
|
139 | return false;
|
140 | if (!areDescriptorsEqual(dependencyDescriptorA, dependencyDescriptorB)) {
|
141 | return false;
|
142 | }
|
143 | }
|
144 | return true;
|
145 | }
|
146 | exports.areVirtualPackagesEquivalent = areVirtualPackagesEquivalent;
|
147 | function parseIdent(string) {
|
148 | const ident = tryParseIdent(string);
|
149 | if (!ident)
|
150 | throw new Error(`Invalid ident (${string})`);
|
151 | return ident;
|
152 | }
|
153 | exports.parseIdent = parseIdent;
|
154 | function tryParseIdent(string) {
|
155 | const match = string.match(/^(?:@([^/]+?)\/)?([^/]+)$/);
|
156 | if (!match)
|
157 | return null;
|
158 | const [, scope, name] = match;
|
159 | const realScope = typeof scope !== `undefined`
|
160 | ? scope
|
161 | : null;
|
162 | return makeIdent(realScope, name);
|
163 | }
|
164 | exports.tryParseIdent = tryParseIdent;
|
165 | function parseDescriptor(string, strict = false) {
|
166 | const descriptor = tryParseDescriptor(string, strict);
|
167 | if (!descriptor)
|
168 | throw new Error(`Invalid descriptor (${string})`);
|
169 | return descriptor;
|
170 | }
|
171 | exports.parseDescriptor = parseDescriptor;
|
172 | function tryParseDescriptor(string, strict = false) {
|
173 | const match = strict
|
174 | ? string.match(/^(?:@([^/]+?)\/)?([^/]+?)(?:@(.+))$/)
|
175 | : string.match(/^(?:@([^/]+?)\/)?([^/]+?)(?:@(.+))?$/);
|
176 | if (!match)
|
177 | return null;
|
178 | const [, scope, name, range] = match;
|
179 | if (range === `unknown`)
|
180 | throw new Error(`Invalid range (${string})`);
|
181 | const realScope = typeof scope !== `undefined`
|
182 | ? scope
|
183 | : null;
|
184 | const realRange = typeof range !== `undefined`
|
185 | ? range
|
186 | : `unknown`;
|
187 | return makeDescriptor(makeIdent(realScope, name), realRange);
|
188 | }
|
189 | exports.tryParseDescriptor = tryParseDescriptor;
|
190 | function parseLocator(string, strict = false) {
|
191 | const locator = tryParseLocator(string, strict);
|
192 | if (!locator)
|
193 | throw new Error(`Invalid locator (${string})`);
|
194 | return locator;
|
195 | }
|
196 | exports.parseLocator = parseLocator;
|
197 | function tryParseLocator(string, strict = false) {
|
198 | const match = strict
|
199 | ? string.match(/^(?:@([^/]+?)\/)?([^/]+?)(?:@(.+))$/)
|
200 | : string.match(/^(?:@([^/]+?)\/)?([^/]+?)(?:@(.+))?$/);
|
201 | if (!match)
|
202 | return null;
|
203 | const [, scope, name, reference] = match;
|
204 | if (reference === `unknown`)
|
205 | throw new Error(`Invalid reference (${string})`);
|
206 | const realScope = typeof scope !== `undefined`
|
207 | ? scope
|
208 | : null;
|
209 | const realReference = typeof reference !== `undefined`
|
210 | ? reference
|
211 | : `unknown`;
|
212 | return makeLocator(makeIdent(realScope, name), realReference);
|
213 | }
|
214 | exports.tryParseLocator = tryParseLocator;
|
215 | function parseRange(range, opts) {
|
216 | const match = range.match(/^([^#:]*:)?((?:(?!::)[^#])*)(?:#((?:(?!::).)*))?(?:::(.*))?$/);
|
217 | if (match === null)
|
218 | throw new Error(`Invalid range (${range})`);
|
219 | const protocol = typeof match[1] !== `undefined`
|
220 | ? match[1]
|
221 | : null;
|
222 | if (typeof (opts === null || opts === void 0 ? void 0 : opts.requireProtocol) === `string` && protocol !== opts.requireProtocol)
|
223 | throw new Error(`Invalid protocol (${protocol})`);
|
224 | else if ((opts === null || opts === void 0 ? void 0 : opts.requireProtocol) && protocol === null)
|
225 | throw new Error(`Missing protocol (${protocol})`);
|
226 | const source = typeof match[3] !== `undefined`
|
227 | ? decodeURIComponent(match[2])
|
228 | : null;
|
229 | if ((opts === null || opts === void 0 ? void 0 : opts.requireSource) && source === null)
|
230 | throw new Error(`Missing source (${range})`);
|
231 | const rawSelector = typeof match[3] !== `undefined`
|
232 | ? decodeURIComponent(match[3])
|
233 | : decodeURIComponent(match[2]);
|
234 | const selector = (opts === null || opts === void 0 ? void 0 : opts.parseSelector)
|
235 | ? querystring_1.default.parse(rawSelector)
|
236 | : rawSelector;
|
237 | const params = typeof match[4] !== `undefined`
|
238 | ? querystring_1.default.parse(match[4])
|
239 | : null;
|
240 | return {
|
241 |
|
242 | protocol,
|
243 |
|
244 | source,
|
245 |
|
246 | selector,
|
247 |
|
248 | params,
|
249 | };
|
250 | }
|
251 | exports.parseRange = parseRange;
|
252 | function parseFileStyleRange(range, { protocol }) {
|
253 | const { selector, params } = parseRange(range, {
|
254 | requireProtocol: protocol,
|
255 | requireBindings: true,
|
256 | });
|
257 | if (typeof params.locator !== `string`)
|
258 | throw new Error(`Assertion failed: Invalid bindings for ${range}`);
|
259 | const parentLocator = parseLocator(params.locator, true);
|
260 | const path = selector;
|
261 | return { parentLocator, path };
|
262 | }
|
263 | exports.parseFileStyleRange = parseFileStyleRange;
|
264 | function encodeUnsafeCharacters(str) {
|
265 | str = str.replace(/%/g, `%25`);
|
266 | str = str.replace(/:/g, `%3A`);
|
267 | str = str.replace(/#/g, `%23`);
|
268 | return str;
|
269 | }
|
270 | function hasParams(params) {
|
271 | if (params === null)
|
272 | return false;
|
273 | return Object.entries(params).length > 0;
|
274 | }
|
275 | function makeRange({ protocol, source, selector, params }) {
|
276 | let range = ``;
|
277 | if (protocol !== null)
|
278 | range += `${protocol}`;
|
279 | if (source !== null)
|
280 | range += `${encodeUnsafeCharacters(source)}#`;
|
281 | range += encodeUnsafeCharacters(selector);
|
282 | if (hasParams(params))
|
283 | range += `::${querystring_1.default.stringify(params)}`;
|
284 | return range;
|
285 | }
|
286 | exports.makeRange = makeRange;
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | function convertToManifestRange(range) {
|
294 | const { params, protocol, source, selector } = parseRange(range);
|
295 | for (const name in params)
|
296 | if (name.startsWith(`__`))
|
297 | delete params[name];
|
298 | return makeRange({ protocol, source, params, selector });
|
299 | }
|
300 | exports.convertToManifestRange = convertToManifestRange;
|
301 | function requirableIdent(ident) {
|
302 | if (ident.scope) {
|
303 | return `@${ident.scope}/${ident.name}`;
|
304 | }
|
305 | else {
|
306 | return `${ident.name}`;
|
307 | }
|
308 | }
|
309 | exports.requirableIdent = requirableIdent;
|
310 | function stringifyIdent(ident) {
|
311 | if (ident.scope) {
|
312 | return `@${ident.scope}/${ident.name}`;
|
313 | }
|
314 | else {
|
315 | return `${ident.name}`;
|
316 | }
|
317 | }
|
318 | exports.stringifyIdent = stringifyIdent;
|
319 | function stringifyDescriptor(descriptor) {
|
320 | if (descriptor.scope) {
|
321 | return `@${descriptor.scope}/${descriptor.name}@${descriptor.range}`;
|
322 | }
|
323 | else {
|
324 | return `${descriptor.name}@${descriptor.range}`;
|
325 | }
|
326 | }
|
327 | exports.stringifyDescriptor = stringifyDescriptor;
|
328 | function stringifyLocator(locator) {
|
329 | if (locator.scope) {
|
330 | return `@${locator.scope}/${locator.name}@${locator.reference}`;
|
331 | }
|
332 | else {
|
333 | return `${locator.name}@${locator.reference}`;
|
334 | }
|
335 | }
|
336 | exports.stringifyLocator = stringifyLocator;
|
337 | function slugifyIdent(ident) {
|
338 | if (ident.scope !== null) {
|
339 | return `@${ident.scope}-${ident.name}`;
|
340 | }
|
341 | else {
|
342 | return ident.name;
|
343 | }
|
344 | }
|
345 | exports.slugifyIdent = slugifyIdent;
|
346 | function slugifyLocator(locator) {
|
347 | const { protocol, selector } = parseRange(locator.reference);
|
348 | const humanProtocol = protocol !== null
|
349 | ? protocol.replace(/:$/, ``)
|
350 | : `exotic`;
|
351 | const humanVersion = semver_1.default.valid(selector);
|
352 | const humanReference = humanVersion !== null
|
353 | ? `${humanProtocol}-${humanVersion}`
|
354 | : `${humanProtocol}`;
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 | const hashTruncate = 10;
|
364 | const slug = locator.scope
|
365 | ? `${slugifyIdent(locator)}-${humanReference}-${locator.locatorHash.slice(0, hashTruncate)}`
|
366 | : `${slugifyIdent(locator)}-${humanReference}-${locator.locatorHash.slice(0, hashTruncate)}`;
|
367 | return fslib_1.toFilename(slug);
|
368 | }
|
369 | exports.slugifyLocator = slugifyLocator;
|
370 | function prettyIdent(configuration, ident) {
|
371 | if (ident.scope) {
|
372 | return `${formatUtils.pretty(configuration, `@${ident.scope}/`, formatUtils.Type.SCOPE)}${formatUtils.pretty(configuration, ident.name, formatUtils.Type.NAME)}`;
|
373 | }
|
374 | else {
|
375 | return `${formatUtils.pretty(configuration, ident.name, formatUtils.Type.NAME)}`;
|
376 | }
|
377 | }
|
378 | exports.prettyIdent = prettyIdent;
|
379 | function prettyRangeNoColors(range) {
|
380 | if (range.startsWith(VIRTUAL_PROTOCOL)) {
|
381 | const nested = prettyRangeNoColors(range.substr(range.indexOf(`#`) + 1));
|
382 | const abbrev = range.substr(VIRTUAL_PROTOCOL.length, VIRTUAL_ABBREVIATE);
|
383 |
|
384 |
|
385 | return false ? `${nested} (virtual:${abbrev})` : `${nested} [${abbrev}]`;
|
386 | }
|
387 | else {
|
388 | return range.replace(/\?.*/, `?[...]`);
|
389 | }
|
390 | }
|
391 | function prettyRange(configuration, range) {
|
392 | return `${formatUtils.pretty(configuration, prettyRangeNoColors(range), formatUtils.Type.RANGE)}`;
|
393 | }
|
394 | exports.prettyRange = prettyRange;
|
395 | function prettyDescriptor(configuration, descriptor) {
|
396 | return `${prettyIdent(configuration, descriptor)}${formatUtils.pretty(configuration, `@`, formatUtils.Type.RANGE)}${prettyRange(configuration, descriptor.range)}`;
|
397 | }
|
398 | exports.prettyDescriptor = prettyDescriptor;
|
399 | function prettyReference(configuration, reference) {
|
400 | return `${formatUtils.pretty(configuration, prettyRangeNoColors(reference), formatUtils.Type.REFERENCE)}`;
|
401 | }
|
402 | exports.prettyReference = prettyReference;
|
403 | function prettyLocator(configuration, locator) {
|
404 | return `${prettyIdent(configuration, locator)}${formatUtils.pretty(configuration, `@`, formatUtils.Type.REFERENCE)}${prettyReference(configuration, locator.reference)}`;
|
405 | }
|
406 | exports.prettyLocator = prettyLocator;
|
407 | function prettyLocatorNoColors(locator) {
|
408 | return `${stringifyIdent(locator)}@${prettyRangeNoColors(locator.reference)}`;
|
409 | }
|
410 | exports.prettyLocatorNoColors = prettyLocatorNoColors;
|
411 | function sortDescriptors(descriptors) {
|
412 | return miscUtils.sortMap(descriptors, [
|
413 | descriptor => stringifyIdent(descriptor),
|
414 | descriptor => descriptor.range,
|
415 | ]);
|
416 | }
|
417 | exports.sortDescriptors = sortDescriptors;
|
418 | function prettyWorkspace(configuration, workspace) {
|
419 | return prettyIdent(configuration, workspace.locator);
|
420 | }
|
421 | exports.prettyWorkspace = prettyWorkspace;
|
422 | function prettyResolution(configuration, descriptor, locator) {
|
423 | const devirtualizedDescriptor = isVirtualDescriptor(descriptor)
|
424 | ? devirtualizeDescriptor(descriptor)
|
425 | : descriptor;
|
426 | if (locator === null) {
|
427 | return `${structUtils.prettyDescriptor(configuration, devirtualizedDescriptor)} → ${formatUtils.pretty(configuration, `✘`, `red`)}`;
|
428 | }
|
429 | else if (devirtualizedDescriptor.identHash === locator.identHash) {
|
430 | return `${structUtils.prettyDescriptor(configuration, devirtualizedDescriptor)} → ${prettyReference(configuration, locator.reference)}`;
|
431 | }
|
432 | else {
|
433 | return `${structUtils.prettyDescriptor(configuration, devirtualizedDescriptor)} → ${prettyLocator(configuration, locator)}`;
|
434 | }
|
435 | }
|
436 | exports.prettyResolution = prettyResolution;
|
437 | function prettyDependent(configuration, locator, descriptor) {
|
438 | if (descriptor === null) {
|
439 | return `${prettyLocator(configuration, locator)}`;
|
440 | }
|
441 | else {
|
442 | return `${prettyLocator(configuration, locator)} (via ${structUtils.prettyRange(configuration, descriptor.range)})`;
|
443 | }
|
444 | }
|
445 | exports.prettyDependent = prettyDependent;
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 | function getIdentVendorPath(ident) {
|
458 | return `node_modules/${requirableIdent(ident)}`;
|
459 | }
|
460 | exports.getIdentVendorPath = getIdentVendorPath;
|