import { spawn } from 'node:child_process'; import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const PORT_FILE = '.terminal-server-port'; async function waitForPortFile(timeout = 10000) { const start = Date.now(); while (Date.now() - start < timeout) { if (fs.existsSync(PORT_FILE)) { return true; } await new Promise(r => setTimeout(r, 100)); } return false; } async function main() { // Clean up any existing port file if (fs.existsSync(PORT_FILE)) { fs.unlinkSync(PORT_FILE); } // Start server console.log('Starting terminal server...'); const server = spawn('node', ['server/index.js'], { stdio: 'inherit', shell: true, }); // Wait for server to be ready await waitForPortFile(); const port = fs.readFileSync(PORT_FILE, 'utf-8').trim(); console.log(`Terminal server ready on port ${port}`); // Set env for vite process.env.TERMINAL_API_URL = `http://localhost:${port}`; // Start vite console.log('Starting Vite dev server...'); const vite = spawn('npx', ['vite'], { stdio: 'inherit', shell: true, env: { ...process.env, TERMINAL_API_URL: `http://localhost:${port}` }, }); // Cleanup on exit process.on('exit', () => { server.kill(); vite.kill(); if (fs.existsSync(PORT_FILE)) { fs.unlinkSync(PORT_FILE); } }); } main().catch(console.error);