Files
5chan/scripts/dev-server-utils.mjs
T

72 lines
1.6 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
import { spawn } from 'node:child_process';
import net from 'node:net';
import path from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export const repoRoot = path.resolve(__dirname, '..');
export const isWindows = process.platform === 'win32';
function checkPort(port) {
return new Promise((resolve) => {
const server = net.createServer();
server.once('error', () => resolve(false));
server.once('listening', () => {
server.close(() => resolve(true));
});
server.listen(port);
});
}
export async function resolvePort(requestedPort) {
let port = requestedPort;
while (!(await checkPort(port))) {
port += 1;
}
return port;
}
export function startVite(host, port) {
const child = spawn('corepack', ['yarn', 'exec', 'vite', '--host', host, '--port', String(port), '--strictPort'], {
cwd: repoRoot,
env: {
...process.env,
PORTLESS: '0',
},
stdio: 'inherit',
});
const forwardSignal = (signal) => {
if (!child.killed) {
child.kill(signal);
}
};
const onSigint = () => forwardSignal('SIGINT');
const onSigterm = () => forwardSignal('SIGTERM');
process.on('SIGINT', onSigint);
process.on('SIGTERM', onSigterm);
child.on('exit', (code, signal) => {
process.off('SIGINT', onSigint);
process.off('SIGTERM', onSigterm);
if (signal) {
process.kill(process.pid, signal);
return;
}
process.exit(code ?? 0);
});
return child;
}