clear_node_modules 1.1.6 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/lib.js +51 -28
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear_node_modules",
3
- "version": "1.1.6",
3
+ "version": "1.2.0",
4
4
  "description": "help you remove all large file of node_modules in dist dir!",
5
5
  "main": "index.js",
6
6
  "directories": {
package/src/lib.js CHANGED
@@ -1,36 +1,59 @@
1
- const fs = require('fs')
2
- const path = require('path')
3
- const rimraf = require('rimraf')
4
- const ora = require('ora')
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const rimraf = require("rimraf");
4
+ const ora = require("ora");
5
5
 
6
- const noop = () => {}
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(disDir) {
10
- const p = path.resolve(disDir)
11
- if (!fs.statSync(p).isDirectory()) return
12
- if (!fs.readdirSync(p).length) return
13
- const { size } = fs.statSync(p)
14
- if (size / 1024 < LIMIT_SIZE) return
15
- const dirList = fs.readdirSync(p)
16
- dirList.forEach(dir => {
17
- const subP = path.resolve(p, dir)
18
- if (dir === NODE_MODULES) {
19
- const dirPath = path.resolve(__filename, subP)
20
- const spinner = ora(`Removing ${dirPath}`).start()
21
- rimraf(subP, noop)
22
- spinner.succeed(`Done ${dirPath}`)
23
- return
21
+ return function clearDir(dirPath) {
22
+ if (!fs.existsSync(dirPath) || !fs.statSync(dirPath).isDirectory()) return;
23
+
24
+ const entries = fs.readdirSync(dirPath);
25
+ if (entries.length === 0) return;
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);
24
43
  }
25
- clearDir(subP)
26
- })
27
- }
44
+ }
45
+ };
28
46
  }
29
47
 
30
- function clearFunc(disDir = './', LIMIT_SIZE = 0, NODE_MODULES = 'node_modules') {
31
- console.log('')
32
- wrap(LIMIT_SIZE, NODE_MODULES)(disDir)
33
- console.log('')
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("");
34
57
  }
35
58
 
36
- module.exports = clearFunc
59
+ module.exports = clearFunc;