|
| 1 | +const { spawn } = require('child_process'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const net = require('net'); |
| 5 | +const { |
| 6 | + hydrateProcessEnv, |
| 7 | + determineBackendPort, |
| 8 | + determineFrontendPort, |
| 9 | + resolveEnvFilePath, |
| 10 | + loadEnvFile, |
| 11 | +} = require('./env-utils'); |
| 12 | + |
| 13 | +function findAvailablePort(preferred, attempts = 20) { |
| 14 | + const ports = []; |
| 15 | + if (preferred && Number(preferred) > 0) { |
| 16 | + const base = Number(preferred); |
| 17 | + for (let i = 0; i < attempts; i += 1) ports.push(base + i); |
| 18 | + } |
| 19 | + ports.push(0); |
| 20 | + |
| 21 | + return new Promise((resolve, reject) => { |
| 22 | + const tryNext = () => { |
| 23 | + if (ports.length === 0) { |
| 24 | + reject(new Error('Unable to find available port for frontend dev server.')); |
| 25 | + return; |
| 26 | + } |
| 27 | + const port = ports.shift(); |
| 28 | + const server = net.createServer(); |
| 29 | + server.once('error', () => { |
| 30 | + server.close(); |
| 31 | + tryNext(); |
| 32 | + }); |
| 33 | + server.listen({ port, host: '0.0.0.0', exclusive: true }, () => { |
| 34 | + const address = server.address(); |
| 35 | + server.close(() => { |
| 36 | + if (address && typeof address === 'object') { |
| 37 | + resolve(address.port); |
| 38 | + } else { |
| 39 | + resolve(port); |
| 40 | + } |
| 41 | + }); |
| 42 | + }); |
| 43 | + }; |
| 44 | + tryNext(); |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +function ensurePortAvailable(port) { |
| 49 | + return new Promise((resolve, reject) => { |
| 50 | + const server = net.createServer(); |
| 51 | + server.once('error', (error) => { |
| 52 | + server.close(); |
| 53 | + reject(new Error(`Port ${port} is unavailable: ${error.message}`)); |
| 54 | + }); |
| 55 | + server.listen({ port, host: '0.0.0.0', exclusive: true }, () => { |
| 56 | + server.close(resolve); |
| 57 | + }); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +function applyEnvFile(filePath) { |
| 62 | + if (!filePath || !fs.existsSync(filePath)) return; |
| 63 | + const vars = loadEnvFile(filePath); |
| 64 | + for (const [key, value] of Object.entries(vars)) { |
| 65 | + if (process.env[key] === undefined) process.env[key] = value; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function writeRuntimeMetadata(filePath, data) { |
| 70 | + const dir = path.dirname(filePath); |
| 71 | + if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); |
| 72 | + fs.writeFileSync(filePath, JSON.stringify({ ...data, updatedAt: new Date().toISOString() }, null, 2)); |
| 73 | +} |
| 74 | + |
| 75 | +async function main() { |
| 76 | + hydrateProcessEnv(); |
| 77 | + applyEnvFile(resolveEnvFilePath()); |
| 78 | + |
| 79 | + const backendPort = determineBackendPort(); |
| 80 | + const preferredFrontendPort = determineFrontendPort(); |
| 81 | + const repoRoot = path.resolve(process.cwd(), '..'); |
| 82 | + const typedAiHome = process.env.TYPEDAI_HOME ? path.resolve(process.env.TYPEDAI_HOME) : null; |
| 83 | + const isDefaultRepo = typedAiHome ? repoRoot === typedAiHome : false; |
| 84 | + |
| 85 | + let frontendPort; |
| 86 | + if (isDefaultRepo) { |
| 87 | + frontendPort = 4200; |
| 88 | + await ensurePortAvailable(frontendPort); |
| 89 | + } else { |
| 90 | + frontendPort = await findAvailablePort(preferredFrontendPort ? Number(preferredFrontendPort) : 4200); |
| 91 | + } |
| 92 | + |
| 93 | + process.env.FRONTEND_PORT = String(frontendPort); |
| 94 | + process.env.UI_URL = `http://localhost:${frontendPort}/`; |
| 95 | + if (!process.env.API_BASE_URL && backendPort) { |
| 96 | + process.env.API_BASE_URL = `http://localhost:${backendPort}/api/`; |
| 97 | + } |
| 98 | + |
| 99 | + console.log('[frontend start] backend port:', backendPort || 'unknown'); |
| 100 | + console.log('[frontend start] frontend port:', frontendPort); |
| 101 | + |
| 102 | + // Generate Angular runtime env file with the resolved variables. |
| 103 | + require('./env.js'); |
| 104 | + |
| 105 | + writeRuntimeMetadata( |
| 106 | + path.resolve(process.cwd(), '../.typedai/runtime/frontend.json'), |
| 107 | + { |
| 108 | + backendPort: backendPort ? Number(backendPort) : undefined, |
| 109 | + frontendPort, |
| 110 | + }, |
| 111 | + ); |
| 112 | + |
| 113 | + const ngArgs = ['serve', '--host', '0.0.0.0', '--port', String(frontendPort)]; |
| 114 | + const child = spawn('ng', ngArgs, { stdio: 'inherit', shell: process.platform === 'win32' }); |
| 115 | + |
| 116 | + child.on('exit', (code, signal) => { |
| 117 | + if (signal) process.kill(process.pid, signal); |
| 118 | + process.exit(typeof code === 'number' ? code : 0); |
| 119 | + }); |
| 120 | + |
| 121 | + process.on('SIGINT', () => child.kill('SIGINT')); |
| 122 | + process.on('SIGTERM', () => child.kill('SIGTERM')); |
| 123 | +} |
| 124 | + |
| 125 | +main().catch((error) => { |
| 126 | + console.error('[frontend start] failed to launch Angular dev server:', error); |
| 127 | + process.exit(1); |
| 128 | +}); |
0 commit comments