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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/lib.js +49 -29
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear_node_modules",
3
- "version": "1.1.8",
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,39 +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)
21
+ return function clearDir(dirPath) {
22
+ if (!fs.existsSync(dirPath) || !fs.statSync(dirPath).isDirectory()) return;
11
23
 
12
- if (!fs.existsSync(p)) return
13
- if (!fs.statSync(p).isDirectory()) return
14
- if (!fs.readdirSync(p).length) return
24
+ const entries = fs.readdirSync(dirPath);
25
+ if (entries.length === 0) return;
15
26
 
16
- const { size } = fs.statSync(p)
17
- if (size / 1024 < LIMIT_SIZE) return
18
- const dirList = fs.readdirSync(p)
19
- dirList.forEach(dir => {
20
- const subP = path.resolve(p, dir)
21
- if (dir === NODE_MODULES) {
22
- const dirPath = path.resolve(__filename, subP)
23
- const spinner = ora(`Removing ${dirPath}`).start()
24
- rimraf(subP, noop)
25
- spinner.succeed(`Done ${dirPath}`)
26
- return
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
- clearDir(subP)
29
- })
30
- }
44
+ }
45
+ };
31
46
  }
32
47
 
33
- function clearFunc(disDir = './', LIMIT_SIZE = 0, NODE_MODULES = 'node_modules') {
34
- console.log('')
35
- wrap(LIMIT_SIZE, NODE_MODULES)(disDir)
36
- 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("");
37
57
  }
38
58
 
39
- module.exports = clearFunc
59
+ module.exports = clearFunc;