Skip to content

Commit 6713f28

Browse files
authored
chore(yarn): make Yarn plugins support monorepos (#2617)
1 parent 8804b1a commit 6713f28

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

.yarn/plugins/clean.cjs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// @ts-check
2-
const { spawnSync } = require("node:child_process");
3-
const fs = require("node:fs");
4-
const path = require("node:path");
52

6-
/** @type {{ name: string; factory: (require: NodeRequire) => unknown; }} */
3+
/** @type {{ name: string; factory: (require: NodeJS.Require) => unknown; }} */
74
module.exports = {
85
name: "plugin-clean",
96
factory: (require) => {
@@ -14,20 +11,35 @@ module.exports = {
1411
static paths = [["clean"]];
1512

1613
async execute() {
17-
const projectRoot = path.dirname(path.dirname(__dirname));
14+
// @ts-expect-error Yarn internal package
15+
const { Configuration, Project } = require("@yarnpkg/core");
16+
// @ts-expect-error Yarn internal package
17+
const { npath } = require("@yarnpkg/fslib");
18+
const { spawnSync } = require("node:child_process");
19+
const fs = require("node:fs");
1820

19-
// Remove the symlink first. On Windows, `git clean` resolves/traverses
20-
// the symlink, causing an infinite loop.
21-
const symlink = path.join(
22-
projectRoot,
23-
"example",
24-
"node_modules",
25-
"react-native-test-app"
21+
const configuration = await Configuration.find(
22+
this.context.cwd,
23+
this.context.plugins
2624
);
27-
fs.rmSync(symlink, { force: true, maxRetries: 3, recursive: true });
25+
const { project } = await Project.find(configuration, this.context.cwd);
26+
27+
// Remove symlinks first. On Windows, `git clean` resolves/traverses
28+
// symlinks, causing an infinite loop.
29+
for (const ws of project.workspaces) {
30+
const rntaPath = npath.join(
31+
npath.fromPortablePath(ws.cwd),
32+
"node_modules",
33+
"react-native-test-app"
34+
);
35+
const stats = fs.lstatSync(rntaPath, { throwIfNoEntry: false });
36+
if (stats?.isSymbolicLink()) {
37+
fs.rmSync(rntaPath);
38+
}
39+
}
2840

2941
spawnSync("git", ["clean", "-dfqx", "--exclude=.yarn/cache"], {
30-
cwd: projectRoot,
42+
cwd: npath.fromPortablePath(project.cwd),
3143
stdio: "inherit",
3244
});
3345
}

.yarn/plugins/undo-bin-sorting.cjs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @ts-check
22
/**
33
* @typedef {{ values: Map<string, unknown>; }} Configuration
4-
* @typedef {{ cwd: string; }} Workspace
4+
* @typedef {{ cwd: string; manifest: { name: { scope?: string; name: string; } } }} Workspace
55
* @typedef {{ configuration: Configuration; cwd: string; workspaces: Workspace[]; }} Project
66
* @typedef {{ mode?: "skip-build" | "update-lockfile"; }} InstallOptions
77
*/
@@ -21,9 +21,9 @@
2121
module.exports = {
2222
name: "plugin-undo-bin-sorting",
2323
factory: (require) => {
24+
// @ts-expect-error Yarn internal package
2425
const { npath } = require("@yarnpkg/fslib");
2526
const fs = require("node:fs");
26-
const path = require("node:path");
2727

2828
const asText = /** @type {const} */ ({ encoding: "utf-8" });
2929

@@ -33,12 +33,21 @@ module.exports = {
3333
hooks: {
3434
/** @type {(project: Project) => void} */
3535
validateProject(project) {
36-
const projectRoot = npath.fromPortablePath(project.cwd);
37-
manifestPath = path.join(projectRoot, "package.json");
38-
orig_rawManifest = fs.readFileSync(manifestPath, asText);
36+
for (const ws of project.workspaces) {
37+
if (ws.manifest.name.name === "react-native-test-app") {
38+
const packagePath = npath.fromPortablePath(ws.cwd);
39+
manifestPath = npath.join(packagePath, "package.json");
40+
orig_rawManifest = fs.readFileSync(manifestPath, asText);
41+
break;
42+
}
43+
}
3944
},
4045
/** @type {(project: Project, options: InstallOptions) => void} */
4146
afterAllInstalled() {
47+
if (!manifestPath) {
48+
return;
49+
}
50+
4251
const rawManifest = fs.readFileSync(manifestPath, asText);
4352
if (rawManifest === orig_rawManifest) {
4453
return;

0 commit comments

Comments
 (0)