-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservices.js
63 lines (56 loc) · 1.54 KB
/
services.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
const nrc = require("node-run-cmd");
const path = require("path");
const sudo = require("sudo-prompt");
const startService = `"${path.join("bin", "start_service.cmd")}"`;
const stopService = `"${path.join("bin", "stop_service.cmd")}"`;
const SEP = "|";
const STATUS_KEY = "STATE";
const STATUS_VALUE = "RUNNING";
const checkStatus = () => {
const p = new Promise((resolve, reject) => {
nrc.run("sc query goodbyedpi", {
onData: (data) => {
let status = false;
data = data.replace(/\r?\n|\r/g, SEP);
const rawArrayData = data.split(SEP).map((x) => x.trim());
rawArrayData.forEach((d) => {
if (d !== undefined && d !== null && d !== "") {
const keyValue = d.split(":").map((x) => x.trim());
const [key, value] = keyValue;
if (key === STATUS_KEY) {
const [, v] = value.split(/\s+/g).map((x) => x.trim());
if (v === STATUS_VALUE) {
status = true;
}
}
}
});
resolve(status);
},
onError: (error) => {
reject(error);
},
});
});
return p;
};
const toggleStatus = (newState) => {
const p = new Promise((resolve, reject) => {
sudo.exec(
newState ? startService : stopService,
{ name: "Toggle Service Status" },
function (error, stdout, stderr) {
if (error) {
reject(error);
} else {
resolve(stdout);
}
}
);
});
return p;
};
module.exports = {
checkStatus,
toggleStatus,
};