import { spawn } from 'node:child_process'; import { fileURLToPath } from 'node:url'; import { existsSync, mkdirSync } from 'node:fs'; const appRoot = fileURLToPath(new URL('../', import.meta.url)); const cli = fileURLToPath(new URL('../node_modules/@theia/cli/bin/theia.js', import.meta.url)); const production = process.env.NODE_ENV === 'production'; if (!existsSync(cli) || !existsSync(`${appRoot}lib/frontend/index.html`)) { // Theia releases may emit frontend assets under lib; use backend generation // as the authoritative build prerequisite below. if (!existsSync(`${appRoot}src-gen/backend/main.js`)) { console.error('Install and build this independent package first: npm install && npm run build'); process.exit(1); } } if (production && process.env.WISHFORGE_GATEWAY_ENFORCED !== 'true') { console.error('Production requires an authenticated workspace gateway. See README.md.'); process.exit(1); } const port = process.env.WISHFORGE_IDE_PORT ?? '4300'; if (!/^\d+$/.test(port) || Number(port) < 1 || Number(port) > 65535) throw new Error('Invalid WISHFORGE_IDE_PORT'); const hostname = process.env.WISHFORGE_IDE_HOST ?? '127.0.0.1'; const workspace = process.env.WISHFORGE_WORKSPACE_PATH ?? `${appRoot}workspace`; if (!process.env.WISHFORGE_WORKSPACE_PATH) mkdirSync(workspace, { recursive: true }); if (!existsSync(workspace)) throw new Error('Configured workspace path does not exist'); const child = spawn( process.execPath, [cli, 'start', workspace, '--hostname', hostname, '--port', port, '--plugins', `local-dir:${appRoot}plugins`], { cwd: appRoot, detached: process.platform !== 'win32', stdio: 'inherit', env: { ...process.env, PYTHONDONTWRITEBYTECODE: '1', WF_IDE_MANAGED_PLUGINS_DIR: `${appRoot}plugins`, // No public/default OCT fallback while project-bound room admission is absent. COLLABORATION_SERVER_URL: process.env.WF_COLLABORATION_GATEWAY_URL ?? 'https://collaboration-disabled.invalid/', }, }, ); for (const signal of ['SIGINT', 'SIGTERM']) process.on(signal, () => { try { if (process.platform !== 'win32' && child.pid) process.kill(-child.pid, signal); else child.kill(signal); } catch { child.kill(signal); } }); child.on('exit', code => process.exit(code ?? 1));