-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
88 lines (75 loc) · 2.01 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
87
88
'use strict';
const alfy = require('alfy');
const psList = require('ps-list');
const pidFromPort = require('pid-from-port');
const util = require('./lib/util');
const loadProcesses = () => {
if (alfy.input.startsWith(':')) {
const search = alfy.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 => alfy.inputMatches(data, 'name'));
};
loadProcesses()
.then(processes => {
const items = processes
.filter(process => !process.name.endsWith(' Helper'))
.map(process => {
const cleanedPath = process.cmd.replace(/\.app\/Contents\/.*$/, '.app');
// TODO: Use the `process.path` property in `ps-list` when implemented there
// The below can be removed then
const pathForIcon = cleanedPath.replace(/ -.*/, ''); // Removes arguments
let subtitle = cleanedPath;
if (process.port) {
// TODO; Use `process.path` property
subtitle = `${process.port} - ${subtitle}`;
}
return {
title: process.name,
autocomplete: process.name,
subtitle,
arg: process.pid,
icon: {
type: 'fileicon',
path: pathForIcon
},
mods: {
shift: {
subtitle: `CPU ${process.cpu}%`
},
alt: {
subtitle: 'Force kill',
arg: JSON.stringify({
alfredworkflow: {
arg: process.pid,
variables: {
force: true
}
}
})
}
}
};
})
.sort(util.sorter);
alfy.output(items);
});