|
| 1 | +/* |
| 2 | +Copyright 2025 New Vector Ltd. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | +Please see LICENSE in the repository root for full details. |
| 6 | +*/ |
| 7 | + |
| 8 | +module.exports = { |
| 9 | + name: "linker", |
| 10 | + factory: (require) => ({ |
| 11 | + hooks: { |
| 12 | + // Yarn's plugin system is very light on documentation. The best we have |
| 13 | + // for this hook is simply the type definition in |
| 14 | + // https://github.com/yarnpkg/berry/blob/master/packages/yarnpkg-core/sources/Plugin.ts |
| 15 | + registerPackageExtensions: async (config, registerPackageExtension) => { |
| 16 | + const { structUtils } = require("@yarnpkg/core"); |
| 17 | + const { parseSyml } = require("@yarnpkg/parsers"); |
| 18 | + const path = require("path"); |
| 19 | + const fs = require("fs"); |
| 20 | + const process = require("process"); |
| 21 | + |
| 22 | + // Create a descriptor that we can use to target our direct dependencies |
| 23 | + const projectPath = config.projectCwd |
| 24 | + .replace(/\\/g, "/") |
| 25 | + .replace("/C:/", "C:/"); |
| 26 | + const manifestPath = path.join(projectPath, "package.json"); |
| 27 | + const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); |
| 28 | + const selfDescriptor = structUtils.parseDescriptor( |
| 29 | + `${manifest.name}@*`, |
| 30 | + true, |
| 31 | + ); |
| 32 | + |
| 33 | + // Load the list of linked packages |
| 34 | + const linksPath = path.join(projectPath, ".links.yaml"); |
| 35 | + let linksFile; |
| 36 | + try { |
| 37 | + linksFile = fs.readFileSync(linksPath, "utf8"); |
| 38 | + } catch (e) { |
| 39 | + return; // File doesn't exist, there's nothing to link |
| 40 | + } |
| 41 | + let links; |
| 42 | + try { |
| 43 | + links = parseSyml(linksFile); |
| 44 | + } catch (e) { |
| 45 | + console.error(".links.yaml has invalid syntax", e); |
| 46 | + process.exit(1); |
| 47 | + } |
| 48 | + |
| 49 | + // Resolve paths and turn them into a Yarn package extension |
| 50 | + const overrides = Object.fromEntries( |
| 51 | + Object.entries(links).map(([name, link]) => [ |
| 52 | + name, |
| 53 | + `portal:${path.resolve(config.projectCwd, link)}`, |
| 54 | + ]), |
| 55 | + ); |
| 56 | + const overrideIdentHashes = new Set(); |
| 57 | + for (const name of Object.keys(overrides)) |
| 58 | + overrideIdentHashes.add( |
| 59 | + structUtils.parseDescriptor(`${name}@*`, true).identHash, |
| 60 | + ); |
| 61 | + |
| 62 | + // Extend our own package's dependencies with these local overrides |
| 63 | + registerPackageExtension(selfDescriptor, { dependencies: overrides }); |
| 64 | + |
| 65 | + // Filter out the original dependencies from the package spec so Yarn |
| 66 | + // actually respects the overrides |
| 67 | + const filterDependencies = (original) => { |
| 68 | + const pkg = structUtils.copyPackage(original); |
| 69 | + pkg.dependencies = new Map( |
| 70 | + Array.from(pkg.dependencies.entries()).filter( |
| 71 | + ([, value]) => !overrideIdentHashes.has(value.identHash), |
| 72 | + ), |
| 73 | + ); |
| 74 | + return pkg; |
| 75 | + }; |
| 76 | + |
| 77 | + // Patch Yarn's own normalizePackage method to use the above filter |
| 78 | + const originalNormalizePackage = config.normalizePackage; |
| 79 | + config.normalizePackage = function (pkg, extensions) { |
| 80 | + return originalNormalizePackage.call( |
| 81 | + this, |
| 82 | + pkg.identHash === selfDescriptor.identHash |
| 83 | + ? filterDependencies(pkg) |
| 84 | + : pkg, |
| 85 | + extensions, |
| 86 | + ); |
| 87 | + }; |
| 88 | + }, |
| 89 | + }, |
| 90 | + }), |
| 91 | +}; |
0 commit comments