Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions .yarn/plugins/@rnx-kit/yarn-plugin-dynamic-extensions.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// @ts-check

/**
* @import { Configuration, Hooks, Manifest, PackageExtensionData, Plugin } from "@yarnpkg/core";
* @import { PortablePath } from "@yarnpkg/fslib";
* @typedef {{ cwd: string; manifest: Manifest["raw"]; }} Workspace;
*/

const DYNAMIC_PACKAGE_EXTENSIONS_KEY = "dynamicPackageExtensions";

// This module *must* be CommonJS because `actions/setup-node` (and probably
// other GitHub actions) does not support ESM. Yarn itself does.
exports.name = "@rnx-kit/yarn-plugin-dynamic-extensions";

/** @type {(require: NodeJS.Require) => Plugin<Hooks>} */
exports.factory = (require) => {
const { Project, SettingsType, structUtils } = require("@yarnpkg/core");
const { npath } = require("@yarnpkg/fslib");

/**
* @param {Configuration} configuration
* @param {PortablePath} projectRoot
* @returns {Promise<((ws: Workspace) => PackageExtensionData | undefined) | void>}
*/
async function loadUserExtensions(configuration, projectRoot) {
const packageExtensions = configuration.get(DYNAMIC_PACKAGE_EXTENSIONS_KEY);
if (typeof packageExtensions !== "string") {
return;
}

const path = require("node:path");
const { pathToFileURL } = require("node:url");

// Make sure we resolve user extensions relative to the source config
const source = configuration.sources.get(DYNAMIC_PACKAGE_EXTENSIONS_KEY);
const sourceDir = source ? npath.dirname(source) : projectRoot;

// On Windows, import paths must include the `file:` protocol.
const root = npath.fromPortablePath(sourceDir);
const url = pathToFileURL(path.resolve(root, packageExtensions));
const external = await import(url.toString());
return external?.default ?? external;
}

/** @type {Plugin<Hooks>["configuration"] & Record<string, unknown>} */
const configuration = {};
configuration[DYNAMIC_PACKAGE_EXTENSIONS_KEY] = {
description: "Path to module providing package extensions",
type: SettingsType.STRING,
};

return {
configuration,
hooks: {
registerPackageExtensions: async (
configuration,
registerPackageExtension
) => {
const { projectCwd } = configuration;
if (!projectCwd) {
return;
}

const getUserExtensions = await loadUserExtensions(
configuration,
projectCwd
);
if (!getUserExtensions) {
return;
}

const { workspace } = await Project.find(configuration, projectCwd);
if (!workspace) {
return;
}

workspace.project.workspacesByCwd.forEach(({ cwd, manifest }) => {
const { name, version, raw } = manifest;
if (!name || !version) {
return;
}

/** @type {Workspace} */
const workspace = { cwd: npath.fromPortablePath(cwd), manifest: raw };
const data = getUserExtensions(workspace);
if (!data) {
return;
}

const descriptor = structUtils.makeDescriptor(name, version);
registerPackageExtension(descriptor, data);
});
},
},
};
};
934 changes: 0 additions & 934 deletions .yarn/releases/yarn-4.6.0.cjs

This file was deleted.

942 changes: 942 additions & 0 deletions .yarn/releases/yarn-4.9.2.cjs

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
dynamicPackageExtensions: ./scripts/dynamic.extensions.mjs

enableGlobalCache: false

nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-4.6.0.cjs
plugins:
- checksum: 672e525b81762c6162366bd3ffec5e86ab8fac2655ef0267047e86a0f32e79a4bde0f170bc30479663f40aa3f006d91f8dc3289f679dd4dc5ae5a5d12ba3ad0b
path: .yarn/plugins/@rnx-kit/yarn-plugin-dynamic-extensions.cjs
spec: "https://raw.githubusercontent.com/microsoft/rnx-kit/main/incubator/yarn-plugin-dynamic-extensions/index.js"

yarnPath: .yarn/releases/yarn-4.9.2.cjs
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,5 @@
]
}
},
"packageManager": "yarn@4.6.0"
"packageManager": "yarn@4.9.2"
}
46 changes: 46 additions & 0 deletions scripts/dynamic.extensions.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

/**
* @param {Object} workspace The package currently being processed
* @param {string} workspace.cwd Path of the current package
* @param {Object} workspace.manifest The content of `package.json`
* @returns {{
* dependencies?: Record<string, string>;
* peerDependencies?: Record<string, string>;
* peerDependenciesMeta?: Record<string, { optional?: boolean }>;
* }}
*/

/**
* These are the dependencies we want to ensure exist in each folder. They will not override
* the dependencies in the package.json if already present.
*/
const scriptDependencies = getScriptFolderDependencies(['typescript', 'eslint', 'just-scripts']);

/**
* Get the dependencies for the script folder, both `devDependencies` and `dependencies`
* @param {string[]} keys - The keys of the dependencies to include
* @returns {Record<string, string>} A map of dependencies for the script folder
*/
function getScriptFolderDependencies(keys) {
const pkgJsonName = path.join(path.dirname(fileURLToPath(import.meta.url)), './package.json');
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonName, 'utf-8'));
const mergedDeps = { ...pkgJson.devDependencies, ...pkgJson.dependencies };
return Object.fromEntries(Object.entries(mergedDeps).filter(([key]) => keys.includes(key)));
}

export default function ({ cwd, manifest }) {
const dependenciesToAdd = {};
Object.keys(scriptDependencies).forEach((key) => {
if ((manifest?.dependencies && manifest.dependencies[key]) || (manifest?.devDependencies && manifest.devDependencies[key])) {
// If the dependency already exists in the package.json, skip it
return;
}
dependenciesToAdd[key] = scriptDependencies[key];
});
return {
dependencies: dependenciesToAdd,
};
}
2 changes: 1 addition & 1 deletion tester_deps/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
yarnPath: ../.yarn/releases/yarn-4.6.0.cjs
yarnPath: ../.yarn/releases/yarn-4.9.2.cjs