-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
48 lines (45 loc) · 1.07 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
var proc = require('child_process');
var Promise = require('bluebird');
var _ = require('lodash');
var flagMap = {
count: 'c',
delimeter: 'd',
full: 'f',
pgroup: 'g',
group: 'G',
'list-name': 'l',
'list-full': 'a',
newest: 'n',
oldest: 'o',
P: 'P',
ppid: 'P',
session: 's',
terminal: 't',
euid: 'u',
uid: 'U',
inverse: 'v',
lightweight: 'w',
exact: 'x',
pidfile: 'F',
logpidfile: 'L',
version: 'V',
help: 'h'
};
exports.exec = function (options) {
var args = _.compact([ options.name ]);
delete options.name;
args = _.compact(args.concat(_.flatten(_.map(options, function (value, flag) {
if (!flagMap[flag]) {
throw new Error('flag not recognized: '+ flag);
}
return [ '-' + flagMap[flag], ((value === true) ? '' : value) ];
}))));
return new Promise(function (resolve, reject) {
proc.exec('pgrep ' + args.join(' '), function (error, stdout, stderr) {
if (error) return reject(error);
resolve(_.compact(_.map(stdout.split('\n'), function (pid) {
return parseInt(pid);
})));
});
});
};