arnaucube
arnaucube/commonroutesAppPublic Self-hosted repository
commonroutesApp/www/lib/ui-leaflet/grunt/utils/getAvailPort.js
www/lib/ui-leaflet/grunt/utils/getAvailPort.jsRaw
 1var exec = require('shelljs').exec;
 2
 3var isWin = /^win/.test(process.platform);
 4
 5var isPortOpen = function (port) {
 6    var cmd;
 7    if (isWin)
 8        cmd = 'netstat -an | find /i ":' + port + '" | find /i "listening"';
 9    else
10        cmd = 'lsof -i:' + port + " | tail -n 1 | awk '{print $2}'";
11    var portResponse = exec(cmd, {
12        silent: true
13    }).output;
14    return portResponse ? false : true;
15}
16
17module.exports = function (startPort) {
18    while (!isPortOpen(startPort)) {
19        startPort += 1;
20    }
21
22    return startPort;
23}