Files
AgendaPro/scripts/preflight.mjs
T

53 lines
1.8 KiB
JavaScript

import net from "node:net";
const MIN_NODE = [22, 5, 0];
const actualNode = process.versions.node.split(".").map(Number);
const isVersionAtLeast = actualNode[0] > MIN_NODE[0]
|| actualNode[0] === MIN_NODE[0] && (actualNode[1] > MIN_NODE[1]
|| actualNode[1] === MIN_NODE[1] && actualNode[2] >= MIN_NODE[2]);
if (!isVersionAtLeast) {
console.error(`AgendaPro requiere Node.js >= ${MIN_NODE.join(".")}; detectado ${process.version}.`);
process.exit(1);
}
try {
await import("node:sqlite");
} catch (error) {
console.error("La instalación de Node no incluye node:sqlite, requerido por AgendaPro.");
console.error(error);
process.exit(1);
}
const portsArg = process.argv.find((arg) => arg.startsWith("--ports="))?.split("=", 2)[1];
const ports = portsArg ? portsArg.split(",").map(Number).filter(Boolean) : [];
function isPortBusy(port) {
return new Promise((resolve) => {
const socket = net.createConnection({ host: "127.0.0.1", port });
let settled = false;
const finish = (busy) => {
if (settled) return;
settled = true;
socket.destroy();
resolve(busy);
};
socket.once("connect", () => finish(true));
socket.once("error", () => finish(false));
socket.setTimeout(500, () => finish(false));
});
}
const busyPorts = [];
for (const port of ports) {
if (await isPortBusy(port)) busyPorts.push(port);
}
if (busyPorts.length) {
console.error(`No se puede iniciar AgendaPro: los puertos ${busyPorts.join(", ")} ya están ocupados.`);
console.error("Cierra el proceso anterior o cambia PORT/VITE_PORT; en Windows puedes localizarlo con: netstat -ano | findstr :<puerto>");
process.exit(1);
}
console.log(`Preflight OK: Node ${process.version}${ports.length ? `; puertos ${ports.join(", ")} disponibles` : ""}.`);