Skip to content

Commit dd6d7d0

Browse files
committed
chore(yarn/clean): make clean command support monorepos
1 parent 8804b1a commit dd6d7d0

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
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
}

0 commit comments

Comments
 (0)