-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (69 loc) · 1.61 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const fs = require('fs')
const userHome = require('user-home')
const columnify = require('columnify')
const files = ['.zsh_history', '.bash_history']
module.exports = limit => {
if (!userHome) {
process.exit() // eslint-disable-line unicorn/no-process-exit
}
const data = {
total: 0
}
files.forEach(file => {
const path = `${userHome}/${file}`
try {
const content = fs.readFileSync(path).toString()
const lines = content.split('\n')
lines.forEach(line => {
try {
const cmd = file.indexOf('zsh') ? line.split(';')[1].split(' ')[0] : line.split(' ')[0]
data.total += 1
if (data[cmd]) {
data[cmd] += 1
} else {
data[cmd] = 1
}
} catch (err) {
}
})
} catch (err) {
}
})
const arr = []
const ret = []
let total
for (const prop in data) {
if (Object.prototype.hasOwnProperty.call(data, prop)) {
arr.push({
cmd: prop,
number: data[prop]
})
}
}
arr.sort((a, b) => {
return b.number - a.number
})
const log = data => {
console.log(
columnify(data, { columns: ['index', 'cmd', 'number', 'percent'] })
)
process.exit() // eslint-disable-line unicorn/no-process-exit
}
arr.forEach((item, index) => {
if (item.cmd === 'total') {
total = item.number
return false
}
const percent = (item.number / total * 100).toFixed(5) + '%'
ret.push({
percent,
cmd: item.cmd,
number: item.number,
index
})
if (index === limit) {
log(ret)
}
})
log(ret)
}