UNPKG

5.12 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.LegacyMigrationResolver = void 0;
4const tslib_1 = require("tslib");
5const fslib_1 = require("@yarnpkg/fslib");
6const parsers_1 = require("@yarnpkg/parsers");
7const semver_1 = tslib_1.__importDefault(require("semver"));
8const MessageName_1 = require("./MessageName");
9const structUtils = tslib_1.__importStar(require("./structUtils"));
10const IMPORTED_PATTERNS = [
11 // These ones come from Git urls
12 [/^(git(?:\+(?:https|ssh))?:\/\/.*(?:\.git)?)#(.*)$/, (version, $0, $1, $2) => `${$1}#commit=${$2}`],
13 // These ones come from the GitHub HTTP endpoints
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 // These ones come from the npm registry
17 [/^https?:\/\/[^/]+\/(?:[^/]+\/)*(?:@[^/]+\/)?([^/]+)\/-\/\1-[^/]+\.tgz(?:#|$)/, version => `npm:${version}`],
18 // The GitHub package registry uses a different style of URLs
19 [/^https:\/\/npm\.pkg\.github\.com\/download\/(?:@[^/]+)\/(?:[^/]+)\/(?:[^/]+)\/(?:[0-9a-f]+)$/, version => `npm:${version}`],
20 // These ones come from the old Yarn offline mirror - we assume they came from npm
21 [/^[^/]+\.tgz#[0-9a-f]+$/, version => `npm:${version}`],
22];
23class 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 // No need to enable it if the lockfile doesn't exist
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 // No need to enable it either if the lockfile is modern
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 // Workspaces don't have the "resolved" key; we can skip them, as their
48 // resolution will be recomputed when needed anyway
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 // This resolver only supports the descriptor -> locator part of the
74 // resolution, not the locator -> package one.
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}
101exports.LegacyMigrationResolver = LegacyMigrationResolver;