|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +/** |
| 4 | + * @import { Configuration, Hooks, Manifest, PackageExtensionData, Plugin } from "@yarnpkg/core"; |
| 5 | + * @typedef {{ cwd: string; manifest: Manifest["raw"]; }} Workspace; |
| 6 | + */ |
| 7 | + |
| 8 | +const DYNAMIC_PACKAGE_EXTENSIONS_KEY = "dynamicPackageExtensions"; |
| 9 | + |
| 10 | +// This module *must* be CommonJS because `actions/setup-node` (and probably |
| 11 | +// other GitHub actions) does not support ESM. Yarn itself does. |
| 12 | +exports.name = "@rnx-kit/yarn-plugin-dynamic-extensions"; |
| 13 | + |
| 14 | +/** @type {(require: NodeJS.Require) => Plugin<Hooks>} */ |
| 15 | +exports.factory = (require) => { |
| 16 | + const { Project, SettingsType, structUtils } = require("@yarnpkg/core"); |
| 17 | + |
| 18 | + /** |
| 19 | + * @param {Configuration} configuration |
| 20 | + * @param {string} projectRoot |
| 21 | + * @returns {Promise<((ws: Workspace) => PackageExtensionData | undefined) | void>} |
| 22 | + */ |
| 23 | + async function loadUserExtensions(configuration, projectRoot) { |
| 24 | + const packageExtensions = configuration.get(DYNAMIC_PACKAGE_EXTENSIONS_KEY); |
| 25 | + if ( |
| 26 | + typeof packageExtensions !== "string" || |
| 27 | + packageExtensions === "false" |
| 28 | + ) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + const path = require("node:path"); |
| 33 | + const { pathToFileURL } = require("node:url"); |
| 34 | + |
| 35 | + // On Windows, import paths must include the `file:` protocol. |
| 36 | + const url = pathToFileURL(path.resolve(projectRoot, packageExtensions)); |
| 37 | + const external = await import(url.toString()); |
| 38 | + return external?.default ?? external; |
| 39 | + } |
| 40 | + |
| 41 | + /** @type {Plugin<Hooks>["configuration"] & Record<string, unknown>} */ |
| 42 | + const configuration = {}; |
| 43 | + configuration[DYNAMIC_PACKAGE_EXTENSIONS_KEY] = { |
| 44 | + description: "Path to module providing package extensions", |
| 45 | + type: SettingsType.STRING, |
| 46 | + }; |
| 47 | + |
| 48 | + return { |
| 49 | + configuration, |
| 50 | + hooks: { |
| 51 | + registerPackageExtensions: async ( |
| 52 | + configuration, |
| 53 | + registerPackageExtension |
| 54 | + ) => { |
| 55 | + const { projectCwd } = configuration; |
| 56 | + if (!projectCwd) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + const { workspace } = await Project.find(configuration, projectCwd); |
| 61 | + if (!workspace) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + // @ts-expect-error Cannot find module or its corresponding type declarations |
| 66 | + const { npath } = require("@yarnpkg/fslib"); |
| 67 | + |
| 68 | + const root = npath.fromPortablePath(projectCwd); |
| 69 | + const getUserExtensions = await loadUserExtensions(configuration, root); |
| 70 | + if (!getUserExtensions) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + workspace.project.workspacesByCwd.forEach(({ cwd, manifest }) => { |
| 75 | + const { name, version, raw } = manifest; |
| 76 | + if (!name || !version) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + /** @type {Workspace} */ |
| 81 | + const workspace = { cwd: npath.fromPortablePath(cwd), manifest: raw }; |
| 82 | + const data = getUserExtensions(workspace); |
| 83 | + if (!data) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const descriptor = structUtils.makeDescriptor(name, version); |
| 88 | + registerPackageExtension(descriptor, data); |
| 89 | + }); |
| 90 | + }, |
| 91 | + }, |
| 92 | + }; |
| 93 | +}; |
0 commit comments