-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (58 loc) · 1.48 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
'use strict';
const arvish = require('arvish');
const psList = require('ps-list');
const pidFromPort = require('pid-from-port');
const loadProcesses = () => {
if (arvish.input.startsWith(':')) {
const search = arvish.input.slice(1);
return Promise.all([
pidFromPort.list(),
psList({all: false})
]).then(result => {
const ports = result[0];
const processList = result[1];
// Swap port and pid
const pidMap = new Map();
for (const entry of ports.entries()) {
const port = String(entry[0]);
if (!port.includes(search)) {
// Filter out results which don't match the search term
continue;
}
pidMap.set(entry[1], String(entry[0]));
}
return processList
.map(process => Object.assign({}, process, {port: pidMap.get(process.pid)}))
.filter(process => Boolean(process.port));
});
}
return psList().then(data => arvish.inputMatches(data, 'name'));
};
loadProcesses()
.then(processes => {
const items = processes
.filter(process => !process.name.endsWith(' Helper'))
.map(process => {
let subtitle = process.cmd;
if (process.port) {
// TODO; Use `process.path` property
subtitle = `${process.port} - ${subtitle}`;
}
return {
title: process.name,
autocomplete: process.name,
subtitle,
arg: process.pid,
mods: {
shift: {
subtitle: `CPU ${process.cpu}%`
},
alt: {
subtitle: 'Force kill',
}
}
};
})
.sort();
arvish.output(items);
});