-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
cli.js
executable file
·75 lines (61 loc) · 1.45 KB
/
cli.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
#!/usr/bin/env node
'use strict'
let args = process.argv.splice(2, process.argv.length)
if (args.length === 0) {
console.log('Please specify a port.')
process.exit(0)
}
let PortHog = require('./index')
let data = []
args.forEach(port => {
let pdata = PortHog(port)
if (pdata !== null) {
pdata.port = port
data.push(pdata)
} else {
data.push({
port: port,
pid: '-',
process: '-',
user: ''
})
}
})
if (data.length === 0) {
console.log('\nNothing running on the specified port(s).\n')
process.exit(0)
}
console.log('\nPORT PID PROCESS\n--------------------------------------------------')
const format = (item) => {
let str = item.port
for (let i = 0; i < (10 - item.port.length); i++) {
str = str + ' '
}
str = str + item.pid
for (let i = 0; i < (10 - item.pid.length); i++) {
str = str + ' '
}
str = str + item.process.replace(/\n{1,10}/, '') + (item.user.trim().length > 0 ? ' (' + item.user + (item.user.trim().toLowerCase() === 'unknown' ? ' User' : '') + ')' : '')
console.log(str)
}
data.forEach(item => {
if (!item.pid) {
try {
let port = item.port
delete item.port
Object.keys(item).forEach(pid => {
format({
port: port,
pid: pid,
process: item[pid].process,
user: item[pid].user
})
})
} catch (e) {
console.log(e)
}
} else {
format(item)
}
})
console.log('\n')