forked from brackets-userland/brackets-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess-utils.ts
More file actions
97 lines (86 loc) · 3.06 KB
/
Copy pathprocess-utils.ts
File metadata and controls
97 lines (86 loc) · 3.06 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
/* eslint-env node */
import { exec } from "child_process";
import * as fs from "fs";
import * as Path from "path";
import * as which from "which";
const isWin = /^win/.test(process.platform);
function fixEOL(str: string) {
return str[str.length - 1] === "\n" ? str.slice(0, -1) : str;
}
function findChildren(arr: Array<{ processid: number, parentprocessid: number }>, pid: number): number[] {
let result: number[] = [];
arr.forEach((obj) => {
if (obj.parentprocessid === pid) {
// add children pid first
result = result.concat(findChildren(arr, obj.processid));
result.push(obj.processid);
}
});
return result;
}
export function killSingleProcess(
pid: number,
callback: (stderr: string | null, stdout: string | null) => void
) {
if (isWin) {
// "taskkill /F /PID 827"
exec("taskkill /F /PID " + pid, (err, stdout, stderr) => {
callback(err ? fixEOL(stderr) : null, err ? null : fixEOL(stdout));
});
} else {
// "kill -9 2563"
exec("kill -9 " + pid, (err, stdout, stderr) => {
callback(err ? fixEOL(stderr) : null, err ? null : fixEOL(stdout));
});
}
}
export function getChildrenOfPid(
pid: number,
callback: (stderr: string | null, pids: number[]) => void
) {
if (isWin) {
exec("wmic process get parentprocessid,processid", (err, stdout, stderr) => {
if (err) {
return callback(fixEOL(stderr), []);
}
const map = fixEOL(stdout).split("\n").map((line) => {
const parts = line.trim().split(/\s+/);
const processid = parseInt(parts.pop() as string, 10);
const parentprocessid = parseInt(parts.pop() as string, 10);
return { processid, parentprocessid };
});
callback(null, findChildren(map, pid));
});
} else {
exec("ps -A -o ppid,pid", (err, stdout, stderr) => {
if (err) {
return callback(fixEOL(stderr), []);
}
const map = fixEOL(stdout).split("\n").map((line) => {
const parts = line.trim().split(/\s+/);
const processid = parseInt(parts.pop() as string, 10);
const parentprocessid = parseInt(parts.pop() as string, 10);
return { processid, parentprocessid };
});
callback(null, findChildren(map, pid));
});
}
}
export function executableExists(
command: string,
callback: (err: NodeJS.ErrnoException | null, exists: boolean, resolvedPath: string | null) => void
) {
which(command, (whichErr, _path) => {
if (whichErr) {
return callback(whichErr, false, null);
}
const path = Path.normalize(_path);
fs.stat(path, (statErr, stats) => {
if (statErr) {
return callback(statErr, false, null);
}
const exists = stats.isFile();
return callback(null, exists, exists ? path : null);
});
});
}