-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_node_modules.js
43 lines (37 loc) · 1003 Bytes
/
clean_node_modules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const fs = require('fs');
const delTargetDir = 'node_modules';
const enterTargetDir = '.';
function deleteFolder(path) {
let files = [];
if (fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach(file => {
const curPath = `${path}/${file}`;
// 本来网上是statSync 我给改成了 lstatSync
if (fs.lstatSync(curPath).isDirectory()) {
deleteFolder(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
function deleteTargetFolder(path, target) {
let files = [];
if (fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach(file => {
const curPath = `${path}/${file}`;
if (fs.lstatSync(curPath).isDirectory()) {
// 同下边 都改成lstatSync
if (file === target) {
deleteFolder(curPath);
} else {
deleteTargetFolder(curPath, target);
}
}
});
}
}
deleteTargetFolder(enterTargetDir, delTargetDir);