-
-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathprocessUtils.js
More file actions
139 lines (118 loc) · 4.01 KB
/
processUtils.js
File metadata and controls
139 lines (118 loc) · 4.01 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var exec = require("child_process").exec,
fs = require("fs"),
Path = require("path"),
which = require("which");
var isWin = /^win/.test(process.platform);
var isMac = process.platform === "darwin";
var noop = function () {};
// Cache for xcode-select CLT check (null = not yet checked)
var _xcodeCliToolsInstalled = null;
function _isXcodeCliToolsInstalled(callback) {
if (_xcodeCliToolsInstalled !== null) {
return callback(_xcodeCliToolsInstalled);
}
exec("xcode-select -p", function (err) {
_xcodeCliToolsInstalled = !err;
callback(_xcodeCliToolsInstalled);
});
}
function fixEOL(str) {
if (str[str.length - 1] === "\n") {
str = str.slice(0, -1);
}
return str;
}
function findChildren(arr, pid) {
var result = [];
arr.forEach(function (obj) {
if (obj.parentprocessid == pid) {
// add children pid first
result = result.concat(findChildren(arr, obj.processid));
result.push(obj.processid);
}
});
return result;
}
function killSingleProcess(pid, callback) {
callback = callback || noop;
pid = pid.toString();
if (isWin) {
// "taskkill /F /PID 827"
exec("taskkill /F /PID " + pid, function (err, stdout, stderr) {
callback(err ? fixEOL(stderr) : undefined, err ? undefined : fixEOL(stdout));
});
} else {
// "kill -9 2563"
exec("kill -9 " + pid, function (err, stdout, stderr) {
callback(err ? fixEOL(stderr) : undefined, err ? undefined : fixEOL(stdout));
});
}
}
function getChildrenOfPid(pid, callback) {
callback = callback || noop;
pid = pid.toString();
if (isWin) {
exec("wmic process get parentprocessid,processid", function (err, stdout, stderr) {
if (err) {
return callback(fixEOL(stderr));
}
stdout = fixEOL(stdout);
var map = stdout.split("\n").map(function (line) {
var parts = line.trim().split(/\s+/);
var o = {};
o.processid = parts.pop();
o.parentprocessid = parts.pop();
return o;
});
callback(null, findChildren(map, pid));
});
} else {
exec("ps -A -o ppid,pid", function (err, stdout, stderr) {
if (err) {
return callback(fixEOL(stderr));
}
stdout = fixEOL(stdout);
var map = stdout.split("\n").map(function (line) {
var parts = line.trim().split(/\s+/);
var o = {};
o.processid = parts.pop();
o.parentprocessid = parts.pop();
return o;
});
callback(null, findChildren(map, pid));
});
}
}
function executableExists(filename, dir, callback) {
if (typeof dir === "function") {
callback = dir;
dir = "";
}
which(filename, function (err, path) {
if (err) {
return callback(err, false);
}
path = Path.normalize(path);
fs.stat(path, function (err, stats) {
if (err) {
return callback(err, false);
}
var exists = stats.isFile();
if (!exists) { path = undefined; }
// On macOS, /usr/bin/git is a shim that triggers an "Install Xcode CLT" dialog
// when spawned if CLT is not installed. Check for CLT before allowing it.
if (exists && isMac && Path.normalize(path) === "/usr/bin/git") {
return _isXcodeCliToolsInstalled(function (installed) {
if (!installed) {
return callback(null, false, undefined);
}
return callback(null, true, path);
});
}
return callback(null, exists, path);
});
});
}
exports.getChildrenOfPid = getChildrenOfPid;
exports.killSingleProcess = killSingleProcess;
exports.executableExists = executableExists;