clear_node_modules 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/lib.js +63 -25
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clear_node_modules",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "help you remove all large file of node_modules in dist dir!",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/mooniitt/clear_node_modules#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"clear_node_modules": "^1.
|
|
30
|
+
"clear_node_modules": "^1.2.0",
|
|
31
31
|
"ora": "^3.0.0",
|
|
32
32
|
"rimraf": "^2.6.2"
|
|
33
33
|
}
|
package/src/lib.js
CHANGED
|
@@ -2,6 +2,9 @@ const fs = require("fs");
|
|
|
2
2
|
const path = require("path");
|
|
3
3
|
const rimraf = require("rimraf");
|
|
4
4
|
const ora = require("ora");
|
|
5
|
+
const { promisify } = require("util");
|
|
6
|
+
|
|
7
|
+
const rimrafAsync = promisify(rimraf);
|
|
5
8
|
|
|
6
9
|
function getDirSize(dir) {
|
|
7
10
|
let total = 0;
|
|
@@ -18,42 +21,77 @@ function getDirSize(dir) {
|
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
function wrap(LIMIT_SIZE, NODE_MODULES) {
|
|
21
|
-
return function clearDir(dirPath) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
spinner.succeed(`Done ${subPath}`);
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
} else if (fs.statSync(subPath).isDirectory()) {
|
|
42
|
-
clearDir(subPath);
|
|
24
|
+
return async function clearDir(dirPath) {
|
|
25
|
+
const tasks = [];
|
|
26
|
+
let totalCount = 0;
|
|
27
|
+
let doneCount = 0;
|
|
28
|
+
|
|
29
|
+
// 先扫描一次,预估总数
|
|
30
|
+
function scan(pathToScan) {
|
|
31
|
+
if (!fs.existsSync(pathToScan) || !fs.statSync(pathToScan).isDirectory())
|
|
32
|
+
return;
|
|
33
|
+
const entries = fs.readdirSync(pathToScan);
|
|
34
|
+
for (const entry of entries) {
|
|
35
|
+
const subPath = path.join(pathToScan, entry);
|
|
36
|
+
if (entry === NODE_MODULES && fs.existsSync(subPath)) {
|
|
37
|
+
totalCount++;
|
|
38
|
+
} else if (fs.statSync(subPath).isDirectory()) {
|
|
39
|
+
scan(subPath);
|
|
40
|
+
}
|
|
43
41
|
}
|
|
44
42
|
}
|
|
43
|
+
|
|
44
|
+
scan(dirPath);
|
|
45
|
+
|
|
46
|
+
async function recurse(currentPath) {
|
|
47
|
+
if (
|
|
48
|
+
!fs.existsSync(currentPath) ||
|
|
49
|
+
!fs.statSync(currentPath).isDirectory()
|
|
50
|
+
)
|
|
51
|
+
return;
|
|
52
|
+
|
|
53
|
+
const entries = fs.readdirSync(currentPath);
|
|
54
|
+
if (entries.length === 0) return;
|
|
55
|
+
|
|
56
|
+
const dirSize = getDirSize(currentPath);
|
|
57
|
+
if (dirSize / 1024 < LIMIT_SIZE) return;
|
|
58
|
+
|
|
59
|
+
for (const entry of entries) {
|
|
60
|
+
const subPath = path.join(currentPath, entry);
|
|
61
|
+
if (entry === NODE_MODULES && fs.statSync(subPath).isDirectory()) {
|
|
62
|
+
const spinner = ora(
|
|
63
|
+
`(${++doneCount}/${totalCount}) Removing ${subPath}`
|
|
64
|
+
).start();
|
|
65
|
+
const task = rimrafAsync(subPath)
|
|
66
|
+
.then(() =>
|
|
67
|
+
spinner.succeed(`(${doneCount}/${totalCount}) Done ${subPath}`)
|
|
68
|
+
)
|
|
69
|
+
.catch((err) =>
|
|
70
|
+
spinner.fail(
|
|
71
|
+
`(${doneCount}/${totalCount}) Failed ${subPath}: ${err.message}`
|
|
72
|
+
)
|
|
73
|
+
);
|
|
74
|
+
tasks.push(task);
|
|
75
|
+
} else if (fs.statSync(subPath).isDirectory()) {
|
|
76
|
+
await recurse(subPath);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
await recurse(dirPath);
|
|
82
|
+
await Promise.all(tasks);
|
|
45
83
|
};
|
|
46
84
|
}
|
|
47
85
|
|
|
48
|
-
function clearFunc(
|
|
86
|
+
async function clearFunc(
|
|
49
87
|
disDir = "./",
|
|
50
88
|
LIMIT_SIZE = 0,
|
|
51
89
|
NODE_MODULES = "node_modules"
|
|
52
90
|
) {
|
|
53
91
|
console.log("");
|
|
54
92
|
const clearDir = wrap(LIMIT_SIZE, NODE_MODULES);
|
|
55
|
-
clearDir(path.resolve(disDir));
|
|
56
|
-
console.log("");
|
|
93
|
+
await clearDir(path.resolve(disDir));
|
|
94
|
+
console.log("\nAll done!\n");
|
|
57
95
|
}
|
|
58
96
|
|
|
59
97
|
module.exports = clearFunc;
|