import type { IIdeSshTarget } from '@git.zone/ide-protocol'; import * as plugins from './plugins.js'; const defaultRemoteTheiaPort = 33990; const defaultOpenCodePort = 4096; class GitZoneIdeElectronShell { private readonly tunnels: plugins.ideSsh.ISshTunnelHandle[] = []; async start() { await plugins.electron.app.whenReady(); this.registerIpcHandlers(); const remoteUrl = getArgValue('--remote-url'); if (remoteUrl) { this.openWorkspaceWindow(remoteUrl); } else { await this.openLauncherWindow(); } plugins.electron.app.on('activate', async () => { if (plugins.electron.BrowserWindow.getAllWindows().length === 0) { await this.openLauncherWindow(); } }); plugins.electron.app.on('before-quit', () => { for (const tunnel of this.tunnels) { void tunnel.dispose(); } }); } private registerIpcHandlers() { plugins.electron.ipcMain.handle('gitzone:list-hosts', async () => { const hosts = await plugins.ideSsh.readSshConfig(); return plugins.ideSsh.listConnectableHosts(hosts).map((host) => ({ alias: host.alias, hostName: host.hostName, user: host.user, port: host.port, })); }); plugins.electron.ipcMain.handle('gitzone:connect', async (_event, input: IConnectInput) => { const localPort = await plugins.ideSsh.findFreePort(); const target: IIdeSshTarget = { id: input.hostAlias, hostAlias: input.hostAlias, workspacePath: input.workspacePath, }; const opencodePassword = plugins.crypto.randomBytes(24).toString('base64url'); const remoteTheiaPort = input.remoteTheiaPort ?? defaultRemoteTheiaPort; const opencodePort = input.openCodePort ?? defaultOpenCodePort; const bootstrapCommand = plugins.ideServerInstaller.createRemoteBootstrapCommand({ serverVersion: plugins.electron.app.getVersion(), workspacePath: input.workspacePath, theiaPort: remoteTheiaPort, opencodePort, opencodeUsername: 'opencode', opencodePassword, }); const bootstrapResult = await plugins.ideSsh.runSshCommand(target, bootstrapCommand, { timeoutMs: 30000, batchMode: input.batchMode ?? true, }); if (bootstrapResult.exitCode !== 0) { throw new Error(bootstrapResult.stderr || `Remote bootstrap failed with ${bootstrapResult.exitCode}`); } const tunnel = plugins.ideSsh.startSshTunnel(target, { localPort, remotePort: remoteTheiaPort, batchMode: input.batchMode ?? true, }); this.tunnels.push(tunnel); const url = `http://127.0.0.1:${localPort}`; this.openWorkspaceWindow(url); return { url, localPort, remoteTheiaPort, opencodePort }; }); } private async openLauncherWindow() { const window = new plugins.electron.BrowserWindow({ width: 960, height: 720, title: 'Git.Zone IDE', webPreferences: { contextIsolation: false, nodeIntegration: true, }, }); await window.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(renderLauncherHtml())}`); } private openWorkspaceWindow(url: string) { const window = new plugins.electron.BrowserWindow({ width: 1440, height: 960, title: 'Git.Zone IDE', webPreferences: { contextIsolation: true, nodeIntegration: false, }, }); void window.loadURL(url); } } interface IConnectInput { hostAlias: string; workspacePath: string; remoteTheiaPort?: number; openCodePort?: number; batchMode?: boolean; } const getArgValue = (name: string) => { const index = process.argv.indexOf(name); if (index === -1) { return undefined; } return process.argv[index + 1]; }; const renderLauncherHtml = () => ` Git.Zone IDE

Git.Zone IDE

Connect to an SSH host and open a remote Theia workspace powered by OpenCode server.


    
`; void new GitZoneIdeElectronShell().start();