clear_node_modules 1.2.0 → 1.2.1
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 +38 -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.1",
|
|
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,52 @@ 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
|
-
if (
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
24
|
+
return async function clearDir(dirPath) {
|
|
25
|
+
const tasks = [];
|
|
26
|
+
|
|
27
|
+
async function recurse(currentPath) {
|
|
28
|
+
if (
|
|
29
|
+
!fs.existsSync(currentPath) ||
|
|
30
|
+
!fs.statSync(currentPath).isDirectory()
|
|
31
|
+
)
|
|
32
|
+
return;
|
|
33
|
+
|
|
34
|
+
const entries = fs.readdirSync(currentPath);
|
|
35
|
+
if (entries.length === 0) return;
|
|
36
|
+
|
|
37
|
+
const dirSize = getDirSize(currentPath);
|
|
38
|
+
if (dirSize / 1024 < LIMIT_SIZE) return;
|
|
39
|
+
|
|
40
|
+
for (const entry of entries) {
|
|
41
|
+
const subPath = path.join(currentPath, entry);
|
|
42
|
+
if (entry === NODE_MODULES && fs.statSync(subPath).isDirectory()) {
|
|
43
|
+
const spinner = ora(`Removing ${subPath}`).start();
|
|
44
|
+
const task = rimrafAsync(subPath)
|
|
45
|
+
.then(() => spinner.succeed(`Done ${subPath}`))
|
|
46
|
+
.catch((err) =>
|
|
47
|
+
spinner.fail(`Failed to remove ${subPath}: ${err.message}`)
|
|
48
|
+
);
|
|
49
|
+
tasks.push(task);
|
|
50
|
+
} else if (fs.statSync(subPath).isDirectory()) {
|
|
51
|
+
await recurse(subPath);
|
|
52
|
+
}
|
|
43
53
|
}
|
|
44
54
|
}
|
|
55
|
+
|
|
56
|
+
await recurse(dirPath);
|
|
57
|
+
await Promise.all(tasks);
|
|
45
58
|
};
|
|
46
59
|
}
|
|
47
60
|
|
|
48
|
-
function clearFunc(
|
|
61
|
+
async function clearFunc(
|
|
49
62
|
disDir = "./",
|
|
50
63
|
LIMIT_SIZE = 0,
|
|
51
64
|
NODE_MODULES = "node_modules"
|
|
52
65
|
) {
|
|
53
66
|
console.log("");
|
|
54
67
|
const clearDir = wrap(LIMIT_SIZE, NODE_MODULES);
|
|
55
|
-
clearDir(path.resolve(disDir));
|
|
56
|
-
console.log("");
|
|
68
|
+
await clearDir(path.resolve(disDir));
|
|
69
|
+
console.log("\nAll done!\n");
|
|
57
70
|
}
|
|
58
71
|
|
|
59
72
|
module.exports = clearFunc;
|