Skip to content

Commit 2f16be9

Browse files
authored
Merge pull request #185 from MCS-Lite/bugfix/kill-port
Bugfix/kill port
2 parents 1868042 + 65d414f commit 2f16be9

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ script:
2727
- ls . && cd ../..
2828

2929
after_success:
30-
- ./node_modules/.bin/codecov
30+
- ./node_modules/.bin/codecov
3131

3232
cache:
3333
directories:

adminServer/controllers/service.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ var os = require('os');
22
var fs = require('fs');
33
var path = require('path');
44
var exec = require('child_process').exec;
5+
var kill = require('cross-port-killer').kill;
56
var $rest = require('../../configs/rest');
67
var $wot = require('../../configs/wot');
78
var $stream = require('../../configs/stream');
89
var $admin = require('../../configs/admin');
910

11+
1012
module.exports = function ($db) {
1113

1214
var startService = function(req, res, next) {
@@ -16,7 +18,6 @@ module.exports = function ($db) {
1618
};
1719

1820
var stopService = function(req, res, next) {
19-
var kill = require('kill-port');
2021
kill($rest.port);
2122
kill($wot.port);
2223
kill($stream.serverPort);

index.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ global.logs = '';
33
global.console.log = function(x){
44
global.logs += JSON.stringify(x) + '\n';
55
process.stdout.write(util.format.apply(util, arguments));
6-
};
6+
};
77

88
var fs = require('fs');
99
var gui = require('nw.gui');
@@ -12,6 +12,22 @@ var nwPath = process.execPath;
1212
var nwDir = path.dirname(nwPath);
1313
var exec = require('child_process').exec;
1414
var spawn = require('child_process').spawn;
15+
var execSync = require('child_process').execSync;
16+
17+
function kill (port) {
18+
if (!Number.parseInt(port)) {
19+
return Promise.reject(new Error('Invalid argument provided for port'))
20+
}
21+
if (process.platform == "darwin") {
22+
return execSync(`lsof -i tcp:${port} | grep LISTEN | awk '{print $2}' | xargs kill -9`);
23+
console.log("run on MAC");
24+
} else {
25+
return spawn('cmd.exe', ['for', '/f', '"tokens=5"', '%a', 'in', '(\'netstat', '-aon', ,'|', 'findstr', ':${port}', '|', 'find', '"LISTENING"\')', 'do', 'taskkill', '/f', '/pid', '%a']);
26+
console.log("run on Windows");
27+
}
28+
29+
}
30+
1531

1632
var $ = function (selector) {
1733
return document.querySelector(selector);
@@ -30,27 +46,28 @@ function startNode(path) {
3046

3147
if (process.env.NODE_ENV === 'dev' && /^win/.test(process.platform)) nodePath = global.__dirname + '\\node\\win32\\node.exe';
3248
// if (process.env.NODE_ENV === 'dev' && /^darwin/.test(process.platform)) nodePath = global.__dirname + '/node/osx64/node';
33-
49+
3450
if (process.env.NODE_ENV != 'dev' && /^win/.test(process.platform)) {
3551
nodePath = nwDir + '\\node.exe';
3652
}
3753

3854
if (process.env.NODE_ENV != 'dev' && process.platform === 'darwin') {
39-
var folderDir = require(global.__dirname + '/config').path;
55+
var folderDir = require(global.__dirname + '/config').path;
4056
nodePath = folderDir + '/node';
4157
}
4258

59+
console.log("nodePath is " + nodePath);
4360
var lite_server = spawn(nodePath, [path]);
4461

45-
lite_server.stdout.on('data', function (data) {
62+
lite_server.stdout.on('data', function (data) {
4663
console.log(data.toString());
4764
});
4865

49-
lite_server.stderr.on('data', function (data) {
50-
console.log(data.toString());
66+
lite_server.stderr.on('data', function (data) {
67+
console.log(data.toString());
5168
});
52-
53-
lite_server.on('close', function (status) {
69+
70+
lite_server.on('close', function (status) {
5471
console.log("Terminal MCS Lite server:" + status);
5572
});
5673
}
@@ -98,7 +115,7 @@ function initApp() {
98115
adminServer = require(folderDir + '\\adminServer\\index');
99116
$admin = require(folderDir + '\\configs\\admin');
100117
}
101-
118+
102119
adminServer.listen($admin.port);
103120
var win = gui.Window.get();
104121
win.show();
@@ -127,14 +144,13 @@ function initApp() {
127144
$admin = require(folderDir + '/configs/admin');
128145
}
129146
}
130-
var kill = require('kill-port');
131147
kill($rest.port);
132148
kill($wot.port);
133149
kill($stream.serverPort);
134150
kill($stream.rtmpServerPort);
135151
win.close(true);
136152
});
137-
153+
138154
if (process.env.NODE_ENV === 'dev') {
139155
document.body.innerHTML += '<iframe frameborder="0" src="http://' + $admin.host + ':' + $admin.port + '/login' + '" style="width: 100%; height: 580px; overflow: auto;" nwdisable nwfaketop>';
140156
// document.body.innerHTML += '<iframe frameborder="0" src="' + $admin.webClient.redirect.dev + '" style="width: 100%; height: 580px; overflow: auto;" nwdisable nwfaketop>';

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"jsonschema": "^1.1.1",
4848
"jsonwebtoken": "^7.2.1",
4949
"lodash": "^4.17.4",
50-
"kill-port": "^1.0.0",
5150
"mcs-lite-admin-web": "^0.1.9",
5251
"mcs-lite-introduction": "^0.2.7",
5352
"mcs-lite-mobile-web": "^0.4.11",
@@ -61,7 +60,8 @@
6160
"superagent": "^3.5.0",
6261
"utils-merge": "^1.0.0",
6362
"wotcity.io": "^0.8.14",
64-
"youmeb-generator": "0.0.1"
63+
"youmeb-generator": "0.0.1",
64+
"cross-port-killer": "^1.0.1"
6565
},
6666
"devDependencies": {
6767
"babel-eslint": "^7.2.3",

0 commit comments

Comments
 (0)