UNPKG

4.91 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 // These ones come from the old Yarn offline mirror - we assume they came from npm
19 [/^[^/]+\.tgz#[0-9a-f]+$/, version => `npm:${version}`],
20];
21class LegacyMigrationResolver {
22 constructor() {
23 this.resolutions = null;
24 }
25 async setup(project, { report }) {
26 const lockfilePath = fslib_1.ppath.join(project.cwd, project.configuration.get(`lockfileFilename`));
27 // No need to enable it if the lockfile doesn't exist
28 if (!fslib_1.xfs.existsSync(lockfilePath))
29 return;
30 const content = await fslib_1.xfs.readFilePromise(lockfilePath, `utf8`);
31 const parsed = parsers_1.parseSyml(content);
32 // No need to enable it either if the lockfile is modern
33 if (Object.prototype.hasOwnProperty.call(parsed, `__metadata`))
34 return;
35 const resolutions = this.resolutions = new Map();
36 for (const key of Object.keys(parsed)) {
37 let descriptor = structUtils.tryParseDescriptor(key);
38 if (!descriptor) {
39 report.reportWarning(MessageName_1.MessageName.YARN_IMPORT_FAILED, `Failed to parse the string "${key}" into a proper descriptor`);
40 continue;
41 }
42 if (semver_1.default.validRange(descriptor.range))
43 descriptor = structUtils.makeDescriptor(descriptor, `npm:${descriptor.range}`);
44 const { version, resolved } = parsed[key];
45 // Workspaces don't have the "resolved" key; we can skip them, as their
46 // resolution will be recomputed when needed anyway
47 if (!resolved)
48 continue;
49 let reference;
50 for (const [pattern, matcher] of IMPORTED_PATTERNS) {
51 const match = resolved.match(pattern);
52 if (match) {
53 reference = matcher(version, ...match);
54 break;
55 }
56 }
57 if (!reference) {
58 report.reportWarning(MessageName_1.MessageName.YARN_IMPORT_FAILED, `${structUtils.prettyDescriptor(project.configuration, descriptor)}: Only some patterns can be imported from legacy lockfiles (not "${resolved}")`);
59 continue;
60 }
61 const resolution = structUtils.makeLocator(descriptor, reference);
62 resolutions.set(descriptor.descriptorHash, resolution);
63 }
64 }
65 supportsDescriptor(descriptor, opts) {
66 if (!this.resolutions)
67 return false;
68 return this.resolutions.has(descriptor.descriptorHash);
69 }
70 supportsLocator(locator, opts) {
71 // This resolver only supports the descriptor -> locator part of the
72 // resolution, not the locator -> package one.
73 return false;
74 }
75 shouldPersistResolution(locator, opts) {
76 throw new Error(`Assertion failed: This resolver doesn't support resolving locators to packages`);
77 }
78 bindDescriptor(descriptor, fromLocator, opts) {
79 return descriptor;
80 }
81 getResolutionDependencies(descriptor, opts) {
82 return [];
83 }
84 async getCandidates(descriptor, dependencies, opts) {
85 if (!this.resolutions)
86 throw new Error(`Assertion failed: The resolution store should have been setup`);
87 const resolution = this.resolutions.get(descriptor.descriptorHash);
88 if (!resolution)
89 throw new Error(`Assertion failed: The resolution should have been registered`);
90 return [resolution];
91 }
92 async getSatisfying(descriptor, references, opts) {
93 return null;
94 }
95 async resolve(locator, opts) {
96 throw new Error(`Assertion failed: This resolver doesn't support resolving locators to packages`);
97 }
98}
99exports.LegacyMigrationResolver = LegacyMigrationResolver;