-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·64 lines (49 loc) · 1.28 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env node
const columnify = require('columnify')
const filesize = require('filesize')
const fs = require('fs')
var directories = {}
function getDirectorySize (dir) {
var files = fs.readdirSync(dir)
var size = 0
files.forEach(path => {
var path = dir + '/' + path
var stats
try {
stats = fs.statSync(path)
} catch (err) {
// printing too many long filenames can overwhelm the GC
if (err.code === 'ENAMETOOLONG') {
console.error('ENAMETOOLONG: name too long (filename omitted)')
} else {
console.error(err.message)
}
return
}
if (stats.isDirectory()) {
size += getDirectorySize(path)
} else {
size += stats.size
}
})
directories[dir] = size
return size
}
function findMyHdBro (dir, count) {
if (!dir) { dir = '.' }
if (!count) { count = 20 }
var hd = {}
getDirectorySize(dir)
var sorted = Object.keys(directories).sort((a, b) => {
return directories[b] - directories[a]
})
sorted.slice(0, count).forEach(dir => {
hd[dir] = filesize(directories[dir])
})
return hd
}
if (require.main === module) {
var hd = findMyHdBro(process.argv[2], process.argv[3])
console.log(columnify(hd, { columns: ['DIRECTORY', 'SIZE'] }))
}
module.exports = findMyHdBro