@nxextensions/firebase-cypress
Version:
An NX Plugin for Firebase Applications that would like to use emulators for E2E testing with Cypress
44 lines • 1.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.killPort = killPort;
const tslib_1 = require("tslib");
const shell_exec_1 = tslib_1.__importDefault(require("shell-exec"));
function killPort(port, method = 'tcp') {
port = Number.parseInt(String(port));
if (!port || isNaN(port)) {
return Promise.reject(new Error('Invalid port number received'));
}
if (process.platform === 'win32') {
return (0, shell_exec_1.default)('netstat -ano')
.then((res) => {
const { stdout } = res;
if (!stdout)
return res;
const lines = stdout.split('\n');
const lineWithLocalPortRegex = new RegExp(`^ *${method.toLocaleUpperCase()} *[^ ]*:${port}`, 'gm');
const linesWithLocalPort = lines.filter((line) => line.match(lineWithLocalPortRegex));
const pids = linesWithLocalPort.reduce((acc, line) => {
const match = line.match(/(\d*)\w*(\n|$)/gm);
return match && match[0] && !acc.includes(match[0])
? acc.concat(match[0])
: acc;
}, []);
return (0, shell_exec_1.default)(`TaskKill /F /PID ${pids.join(' /PID ')}`);
})
.then();
}
return (0, shell_exec_1.default)('lsof -i -P')
.then((res) => {
const { stdout } = res;
if (!stdout)
return res;
const lines = stdout.split('\n');
const existProcess = lines.filter((line) => line.match(new RegExp(`:*${port}`)))
.length > 0;
if (!existProcess)
return Promise.reject(new Error('No process running on port'));
return (0, shell_exec_1.default)(`lsof -i ${method === 'udp' ? 'udp' : 'tcp'}:${port} | grep ${method === 'udp' ? 'UDP' : 'LISTEN'} | awk '{print $2}' | xargs kill -9`);
})
.then();
}
//# sourceMappingURL=kill-port.js.map