forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean-node-modules.js
38 lines (34 loc) · 1.09 KB
/
clean-node-modules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import fs from "node:fs";
import path from "node:path";
import url from "node:url";
const __filename = url.fileURLToPath(new URL(import.meta.url));
const __dirname = path.dirname(__filename);
const repoRoot = path.resolve(__dirname, "..");
/**
* @param {string} p
* @returns {Iterable<string>}
*/
function* iterateNodeModules(p) {
const dirents = fs.readdirSync(p, { withFileTypes: true });
for (const dirent of dirents) {
if (dirent.isDirectory()) {
const subDir = path.join(p, dirent.name);
if (dirent.name == "node_modules") {
yield subDir;
continue;
}
yield* iterateNodeModules(subDir);
}
}
}
/**
* @param {string} p
*/
function rimraf(p) {
// The rimraf package uses maxRetries=10 on Windows, but Node's fs.rm does not have that special case.
return fs.rmSync(p, { recursive: true, force: true, maxRetries: process.platform === "win32" ? 10 : 0 });
}
for (const nodeModules of iterateNodeModules(repoRoot)) {
console.log(path.relative(repoRoot, nodeModules));
rimraf(nodeModules);
}