-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathstop_explorer.js
63 lines (59 loc) · 2.06 KB
/
stop_explorer.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 settings = require('../lib/settings');
const { exec } = require('child_process');
function validate_port(port) {
if (port == null || typeof port !== 'number' || port < 1 || port > 65535)
return false;
else
return true;
}
function check_webserver_running(cb) {
// determine operating system
switch (process.platform) {
case 'win32':
// windows
exec(`for /f "tokens=5" %a in ('netstat -aon ^| findstr :${settings.webserver.port} ^| findstr LISTENING') do @echo %~nxa`, (err, stdout, stderr) => {
// check if the port is open
if (stdout != null && stdout != '') {
// split the results in case there are multiple (usually because of ipv4 and ipv6)
split = stdout.split('\n');
// return the kill cmd
return cb(`taskkill /f /pid ${split[0]}`);
} else
return cb(null);
});
break;
default:
// linux, macos, etc.
exec(`lsof -t -i:${settings.webserver.port}`, (err, stdout, stderr) => {
// check if the port is open
if (stdout != null && stdout != '') {
// return the kill cmd
return cb(`kill -2 ${stdout.trim()}`);
} else
return cb(null);
});
}
}
// check if the webserver.port value is a valid port #
if (validate_port(settings.webserver.port) == true) {
// check if the server is currently running
check_webserver_running(function(killcmd) {
// check return value
if (killcmd != null) {
// send a kill signal to the process that is currently using the explorer's server port
exec(killcmd, (err, stdout, stderr) => {
// show shutdown msg
console.log(`${settings.localization.explorer_shutting_down}.. ${settings.localization.please_wait}..`);
process.exit(0);
});
} else {
// webserver is not running
console.log('Error: Cannot stop explorer because it is not currently running');
process.exit(1);
}
});
} else {
// invalid port number
console.log('Error: webserver.port value not found in settings.json.');
process.exit(1);
}