clear_node_modules 1.1.8 → 1.2.0
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 +1 -1
- package/src/lib.js +49 -29
package/package.json
CHANGED
package/src/lib.js
CHANGED
|
@@ -1,39 +1,59 @@
|
|
|
1
|
-
const fs = require(
|
|
2
|
-
const path = require(
|
|
3
|
-
const rimraf = require(
|
|
4
|
-
const ora = require(
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const rimraf = require("rimraf");
|
|
4
|
+
const ora = require("ora");
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
function getDirSize(dir) {
|
|
7
|
+
let total = 0;
|
|
8
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
9
|
+
for (const entry of entries) {
|
|
10
|
+
const fullPath = path.join(dir, entry.name);
|
|
11
|
+
if (entry.isDirectory()) {
|
|
12
|
+
total += getDirSize(fullPath);
|
|
13
|
+
} else {
|
|
14
|
+
total += fs.statSync(fullPath).size;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return total;
|
|
18
|
+
}
|
|
7
19
|
|
|
8
20
|
function wrap(LIMIT_SIZE, NODE_MODULES) {
|
|
9
|
-
return function clearDir(
|
|
10
|
-
|
|
21
|
+
return function clearDir(dirPath) {
|
|
22
|
+
if (!fs.existsSync(dirPath) || !fs.statSync(dirPath).isDirectory()) return;
|
|
11
23
|
|
|
12
|
-
|
|
13
|
-
if (
|
|
14
|
-
if (!fs.readdirSync(p).length) return
|
|
24
|
+
const entries = fs.readdirSync(dirPath);
|
|
25
|
+
if (entries.length === 0) return;
|
|
15
26
|
|
|
16
|
-
const
|
|
17
|
-
if (
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
if (
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
const dirSize = getDirSize(dirPath);
|
|
28
|
+
if (dirSize / 1024 < LIMIT_SIZE) return;
|
|
29
|
+
|
|
30
|
+
for (const entry of entries) {
|
|
31
|
+
const subPath = path.join(dirPath, entry);
|
|
32
|
+
if (entry === NODE_MODULES && fs.statSync(subPath).isDirectory()) {
|
|
33
|
+
const spinner = ora(`Removing ${subPath}`).start();
|
|
34
|
+
rimraf(subPath, (err) => {
|
|
35
|
+
if (err) {
|
|
36
|
+
spinner.fail(`Failed to remove ${subPath}: ${err.message}`);
|
|
37
|
+
} else {
|
|
38
|
+
spinner.succeed(`Done ${subPath}`);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
} else if (fs.statSync(subPath).isDirectory()) {
|
|
42
|
+
clearDir(subPath);
|
|
27
43
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
31
46
|
}
|
|
32
47
|
|
|
33
|
-
function clearFunc(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
48
|
+
function clearFunc(
|
|
49
|
+
disDir = "./",
|
|
50
|
+
LIMIT_SIZE = 0,
|
|
51
|
+
NODE_MODULES = "node_modules"
|
|
52
|
+
) {
|
|
53
|
+
console.log("");
|
|
54
|
+
const clearDir = wrap(LIMIT_SIZE, NODE_MODULES);
|
|
55
|
+
clearDir(path.resolve(disDir));
|
|
56
|
+
console.log("");
|
|
37
57
|
}
|
|
38
58
|
|
|
39
|
-
module.exports = clearFunc
|
|
59
|
+
module.exports = clearFunc;
|