import { spawn } from 'child_process'; import os from 'os'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; const isWin = os.platform() === 'win32'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const runnerDir = path.join(__dirname, '.ryp-runner'); const goCacheDir = path.join(runnerDir, 'go-cache'); const goTmpDir = path.join(runnerDir, 'go-tmp'); async function run() { if (isWin) { process.env.GOROOT = 'C:\\Program Files\\Go'; } fs.mkdirSync(runnerDir, { recursive: true }); fs.mkdirSync(goCacheDir, { recursive: true }); fs.mkdirSync(goTmpDir, { recursive: true }); const exeName = isWin ? '.ryp-runner/server.exe' : '.ryp-runner/server'; const backendEnv = { ...process.env, GOCACHE: goCacheDir, GOROOT: process.env.GOROOT, GOTMPDIR: goTmpDir, }; console.log('Building Go backend...'); const build = spawn('go', ['build', '-o', exeName, './cmd/server'], { cwd: __dirname, env: backendEnv, stdio: 'inherit', shell: false, }); build.on('error', (err) => { console.error('Failed to run go build:', err.message); process.exit(1); }); build.on('close', (code) => { if (code !== 0) { console.error(`Go build failed with exit code ${code}`); process.exit(code); } console.log('Starting Go backend server...'); const serverBin = path.resolve(__dirname, exeName); const runServer = spawn(serverBin, [], { cwd: __dirname, env: backendEnv, stdio: 'inherit', shell: false, }); runServer.on('error', (err) => { console.error('Failed to start backend server:', err.message); process.exit(1); }); runServer.on('close', (exitCode) => { console.log(`Backend server stopped with code ${exitCode}`); process.exit(exitCode ?? 0); }); console.log('Starting Node.js compiler server...'); const runCompiler = spawn('node', ['server/index.js'], { cwd: __dirname, env: process.env, stdio: 'inherit', shell: false, }); runCompiler.on('error', (err) => { console.error('Failed to start Node compiler server:', err.message); }); runCompiler.on('close', (exitCode) => { console.log(`Node compiler server stopped with code ${exitCode}`); }); }); } run();