1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.LegacyMigrationResolver = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const fslib_1 = require("@yarnpkg/fslib");
|
6 | const parsers_1 = require("@yarnpkg/parsers");
|
7 | const semver_1 = tslib_1.__importDefault(require("semver"));
|
8 | const MessageName_1 = require("./MessageName");
|
9 | const structUtils = tslib_1.__importStar(require("./structUtils"));
|
10 | const IMPORTED_PATTERNS = [
|
11 |
|
12 | [/^(git(?:\+(?:https|ssh))?:\/\/.*(?:\.git)?)#(.*)$/, (version, $0, $1, $2) => `${$1}#commit=${$2}`],
|
13 |
|
14 | [/^https:\/\/((?:[^/]+?)@)?codeload\.github\.com\/([^/]+\/[^/]+)\/tar\.gz\/([0-9a-f]+)$/, (version, $0, $1 = ``, $2, $3) => `https://${$1}github.com/${$2}.git#commit=${$3}`],
|
15 | [/^https:\/\/((?:[^/]+?)@)?github\.com\/([^/]+\/[^/]+?)(?:\.git)?#([0-9a-f]+)$/, (version, $0, $1 = ``, $2, $3) => `https://${$1}github.com/${$2}.git#commit=${$3}`],
|
16 |
|
17 | [/^https?:\/\/[^/]+\/(?:[^/]+\/)*(?:@[^/]+\/)?([^/]+)\/-\/\1-[^/]+\.tgz(?:#|$)/, version => `npm:${version}`],
|
18 |
|
19 | [/^https:\/\/npm\.pkg\.github\.com\/download\/(?:@[^/]+)\/(?:[^/]+)\/(?:[^/]+)\/(?:[0-9a-f]+)$/, version => `npm:${version}`],
|
20 |
|
21 | [/^[^/]+\.tgz#[0-9a-f]+$/, version => `npm:${version}`],
|
22 | ];
|
23 | class LegacyMigrationResolver {
|
24 | constructor() {
|
25 | this.resolutions = null;
|
26 | }
|
27 | async setup(project, { report }) {
|
28 | const lockfilePath = fslib_1.ppath.join(project.cwd, project.configuration.get(`lockfileFilename`));
|
29 |
|
30 | if (!fslib_1.xfs.existsSync(lockfilePath))
|
31 | return;
|
32 | const content = await fslib_1.xfs.readFilePromise(lockfilePath, `utf8`);
|
33 | const parsed = parsers_1.parseSyml(content);
|
34 |
|
35 | if (Object.prototype.hasOwnProperty.call(parsed, `__metadata`))
|
36 | return;
|
37 | const resolutions = this.resolutions = new Map();
|
38 | for (const key of Object.keys(parsed)) {
|
39 | let descriptor = structUtils.tryParseDescriptor(key);
|
40 | if (!descriptor) {
|
41 | report.reportWarning(MessageName_1.MessageName.YARN_IMPORT_FAILED, `Failed to parse the string "${key}" into a proper descriptor`);
|
42 | continue;
|
43 | }
|
44 | if (semver_1.default.validRange(descriptor.range))
|
45 | descriptor = structUtils.makeDescriptor(descriptor, `npm:${descriptor.range}`);
|
46 | const { version, resolved } = parsed[key];
|
47 |
|
48 |
|
49 | if (!resolved)
|
50 | continue;
|
51 | let reference;
|
52 | for (const [pattern, matcher] of IMPORTED_PATTERNS) {
|
53 | const match = resolved.match(pattern);
|
54 | if (match) {
|
55 | reference = matcher(version, ...match);
|
56 | break;
|
57 | }
|
58 | }
|
59 | if (!reference) {
|
60 | report.reportWarning(MessageName_1.MessageName.YARN_IMPORT_FAILED, `${structUtils.prettyDescriptor(project.configuration, descriptor)}: Only some patterns can be imported from legacy lockfiles (not "${resolved}")`);
|
61 | continue;
|
62 | }
|
63 | const resolution = structUtils.makeLocator(descriptor, reference);
|
64 | resolutions.set(descriptor.descriptorHash, resolution);
|
65 | }
|
66 | }
|
67 | supportsDescriptor(descriptor, opts) {
|
68 | if (!this.resolutions)
|
69 | return false;
|
70 | return this.resolutions.has(descriptor.descriptorHash);
|
71 | }
|
72 | supportsLocator(locator, opts) {
|
73 |
|
74 |
|
75 | return false;
|
76 | }
|
77 | shouldPersistResolution(locator, opts) {
|
78 | throw new Error(`Assertion failed: This resolver doesn't support resolving locators to packages`);
|
79 | }
|
80 | bindDescriptor(descriptor, fromLocator, opts) {
|
81 | return descriptor;
|
82 | }
|
83 | getResolutionDependencies(descriptor, opts) {
|
84 | return [];
|
85 | }
|
86 | async getCandidates(descriptor, dependencies, opts) {
|
87 | if (!this.resolutions)
|
88 | throw new Error(`Assertion failed: The resolution store should have been setup`);
|
89 | const resolution = this.resolutions.get(descriptor.descriptorHash);
|
90 | if (!resolution)
|
91 | throw new Error(`Assertion failed: The resolution should have been registered`);
|
92 | return [resolution];
|
93 | }
|
94 | async getSatisfying(descriptor, references, opts) {
|
95 | return null;
|
96 | }
|
97 | async resolve(locator, opts) {
|
98 | throw new Error(`Assertion failed: This resolver doesn't support resolving locators to packages`);
|
99 | }
|
100 | }
|
101 | exports.LegacyMigrationResolver = LegacyMigrationResolver;
|