clear_node_modules 1.2.1 → 1.2.3

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 +28 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear_node_modules",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
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
@@ -23,6 +23,25 @@ function getDirSize(dir) {
23
23
  function wrap(LIMIT_SIZE, NODE_MODULES) {
24
24
  return async function clearDir(dirPath) {
25
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
+ }
41
+ }
42
+ }
43
+
44
+ scan(dirPath);
26
45
 
27
46
  async function recurse(currentPath) {
28
47
  if (
@@ -40,11 +59,17 @@ function wrap(LIMIT_SIZE, NODE_MODULES) {
40
59
  for (const entry of entries) {
41
60
  const subPath = path.join(currentPath, entry);
42
61
  if (entry === NODE_MODULES && fs.statSync(subPath).isDirectory()) {
43
- const spinner = ora(`Removing ${subPath}`).start();
62
+ const spinner = ora(
63
+ `(${++doneCount}/${totalCount}) Removing ${subPath}`
64
+ ).start();
44
65
  const task = rimrafAsync(subPath)
45
- .then(() => spinner.succeed(`Done ${subPath}`))
66
+ .then(() =>
67
+ spinner.succeed(`(${doneCount}/${totalCount}) Done ${subPath}`)
68
+ )
46
69
  .catch((err) =>
47
- spinner.fail(`Failed to remove ${subPath}: ${err.message}`)
70
+ spinner.fail(
71
+ `(${doneCount}/${totalCount}) Failed ${subPath}: ${err.message}`
72
+ )
48
73
  );
49
74
  tasks.push(task);
50
75
  } else if (fs.statSync(subPath).isDirectory()) {