-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·144 lines (133 loc) · 3.5 KB
/
index.js
File metadata and controls
executable file
·144 lines (133 loc) · 3.5 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const inquirer = require('inquirer')
const rimraf = require('rimraf')
const ora = require('ora')
const base_path = '/'
function safeReadDirSync(path) {
let dirData = {}
try {
dirData = fs.readdirSync(path)
} catch (ex) {
if (ex.code == 'EACCES' || ex.code == 'EPERM') {
//User does not have permissions, ignore directory
return null
} else throw ex
}
return dirData
}
const output = []
const walker = (p) => {
let _path = base_path
if (p) {
_path = p
}
try {
const data = safeReadDirSync(_path)
data.forEach((fileName) => {
const currentPath = path.join(_path, fileName)
const stat = fs.lstatSync(currentPath)
if (stat.isDirectory()) {
if (currentPath.includes('node_modules')) {
output.push({ path: currentPath, size: stat.size })
} else {
walker(currentPath)
}
}
})
} catch (err) {
console.log(err)
}
}
const removeNodeModule = (path, spinner) => {
try {
spinner.start(`正在删除${path}`)
rimraf.sync(path)
spinner.succeed(`删除${path} 成功!`)
} catch (err) {
spinner.fail(`删除${path} 失败`)
spinner.fail(err)
}
}
const removeNodeModules = (paths, spinner) => {
paths.forEach((path) => removeNodeModule(path, spinner))
}
const selet = (data) => {
return new Promise((resolve, reject) => {
inquirer
.prompt([
{
type: 'checkbox',
message: '请选择想要删除的node_modules',
name: 'node_modules',
choices: data.map((d) => ({ name: d.path, size: d.size })),
validate(answer) {
return true
},
},
])
.then((answers) => {
// console.log(JSON.stringify(answers.node_modules, null, ' '))
resolve(answers.node_modules)
})
.catch((err) => {
reject(err)
})
})
}
//格式化文件大小
function renderSize(value) {
if (null == value || value == '') {
return '0 Bytes'
}
var unitArr = new Array('Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
var index = 0
var srcsize = parseFloat(value)
index = Math.floor(Math.log(srcsize) / Math.log(1024))
var size = srcsize / Math.pow(1024, index)
size = size.toFixed(2) //保留的小数位数
return size + unitArr[index]
}
const calcTotalSize = (seleted) => {
const sizeArr = output
.filter((o) => {
return seleted.find((select) => select === o.path)
})
.map((o) => o.size)
const totalSize = sizeArr.reduce((a, b) => a + b, 0)
return renderSize(totalSize)
}
const getWorkingPath = () => {
try {
const userInputPath = process.argv[2]
const stat = fs.lstatSync(userInputPath)
if (stat.isDirectory()) {
return userInputPath
}
} catch (err) {
return process.cwd()
}
}
const run = async () => {
const workingPath = getWorkingPath()
const spinner = ora('开始检查文件夹').start()
try {
walker(workingPath)
spinner.info('文件夹检查完成')
const result = await selet(output)
if (result.length > 0) {
spinner.start('开始删除选择的node_modules...')
removeNodeModules(result, spinner)
spinner.succeed('执行成功~')
spinner.info(`共释放${calcTotalSize(result)} 的硬盘空间`)
} else {
spinner.info('没有选择要删除的node_modules')
spinner.info('执行结束~')
}
} catch (err) {
spinner.fail('执行失败')
console.log(err)
}
}
run()