omnichannel-backend/scripts/ensure-port-free.js
Rafael Lopes 8790ce70d0
All checks were successful
Deploy Dev / deploy (push) Successful in 45s
PERF: Criado script para manter a porta 3001 sempre disponível
2026-05-19 09:28:26 -03:00

90 lines
2.1 KiB
JavaScript

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
function loadEnvFile(filePath) {
if (!fs.existsSync(filePath)) return {};
return fs
.readFileSync(filePath, 'utf8')
.split(/\r?\n/)
.reduce((acc, line) => {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) return acc;
const separatorIndex = trimmed.indexOf('=');
if (separatorIndex === -1) return acc;
const key = trimmed.slice(0, separatorIndex).trim();
const value = trimmed.slice(separatorIndex + 1).trim();
acc[key] = value;
return acc;
}, {});
}
function getConfiguredPort() {
const env = {
...loadEnvFile(path.resolve(process.cwd(), '.env.development')),
...process.env,
};
return Number(env.PORT || env.BACKEND_PORT || 3001);
}
function getWindowsPids(port) {
const output = execSync(`netstat -ano -p tcp | findstr :${port}`, {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
});
return Array.from(
new Set(
output
.split(/\r?\n/)
.filter((line) => line.includes('LISTENING'))
.map((line) => line.trim().split(/\s+/).at(-1))
.filter(Boolean)
.filter((pid) => pid !== String(process.pid)),
),
);
}
function getUnixPids(port) {
const output = execSync(`lsof -ti tcp:${port} -sTCP:LISTEN`, {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
});
return Array.from(
new Set(
output
.split(/\r?\n/)
.map((pid) => pid.trim())
.filter(Boolean)
.filter((pid) => pid !== String(process.pid)),
),
);
}
function killPid(pid) {
if (process.platform === 'win32') {
execSync(`taskkill /PID ${pid} /F /T`, { stdio: 'ignore' });
return;
}
execSync(`kill -TERM ${pid}`, { stdio: 'ignore' });
}
const port = getConfiguredPort();
try {
const pids = process.platform === 'win32' ? getWindowsPids(port) : getUnixPids(port);
if (!pids.length) {
process.exit(0);
}
pids.forEach(killPid);
console.log(`Porta ${port} liberada. Processo(s) encerrado(s): ${pids.join(', ')}`);
} catch {
process.exit(0);
}