import { mkdir } from 'node:fs/promises' import path from 'node:path' const desktopRoot = path.resolve(import.meta.dir, '..') const repoRoot = path.resolve(desktopRoot, '..') const binariesDir = path.join(desktopRoot, 'src-tauri', 'binaries') const targetTriple = process.env.TAURI_ENV_TARGET_TRIPLE || process.env.CARGO_BUILD_TARGET || (await detectHostTriple()) const bunTarget = mapTargetTripleToBun(targetTriple) await mkdir(binariesDir, { recursive: true }) await compileExecutable({ entrypoint: path.join(desktopRoot, 'sidecars/server-launcher.ts'), outfileBase: path.join(binariesDir, `claude-server-${targetTriple}`), productName: 'Claude Code Server', bunTarget, }) await compileExecutable({ entrypoint: path.join(desktopRoot, 'sidecars/cli-launcher.ts'), outfileBase: path.join(binariesDir, `claude-cli-${targetTriple}`), productName: 'Claude Code CLI', bunTarget, }) console.log(`[build-sidecars] Built desktop sidecars for ${targetTriple} (${bunTarget})`) async function detectHostTriple() { const proc = Bun.spawn(['rustc', '-vV'], { cwd: repoRoot, stdout: 'pipe', stderr: 'pipe', }) const stdout = await new Response(proc.stdout).text() const stderr = await new Response(proc.stderr).text() const exitCode = await proc.exited if (exitCode !== 0) { throw new Error(`[build-sidecars] rustc -vV failed: ${stderr || stdout}`) } const hostLine = stdout .split('\n') .map((line) => line.trim()) .find((line) => line.startsWith('host: ')) if (!hostLine) { throw new Error('[build-sidecars] Could not detect Rust host triple') } return hostLine.replace('host: ', '') } function mapTargetTripleToBun(triple: string) { switch (triple) { case 'aarch64-apple-darwin': return 'bun-darwin-arm64' case 'x86_64-apple-darwin': return 'bun-darwin-x64' case 'x86_64-pc-windows-msvc': return 'bun-windows-x64' case 'aarch64-pc-windows-msvc': return 'bun-windows-arm64' case 'x86_64-unknown-linux-gnu': return 'bun-linux-x64-baseline' case 'aarch64-unknown-linux-gnu': return 'bun-linux-arm64' case 'x86_64-unknown-linux-musl': return 'bun-linux-x64-musl' case 'aarch64-unknown-linux-musl': return 'bun-linux-arm64-musl' default: throw new Error(`[build-sidecars] Unsupported target triple: ${triple}`) } } async function compileExecutable({ entrypoint, outfileBase, productName, bunTarget, }: { entrypoint: string outfileBase: string productName: string bunTarget: string }) { const result = await Bun.build({ entrypoints: [entrypoint], minify: false, sourcemap: 'none', target: 'bun', compile: { target: bunTarget, outfile: outfileBase, autoloadTsconfig: true, autoloadPackageJson: true, windows: { title: productName, publisher: 'Claude Code', description: productName, hideConsole: true, }, }, }) if (!result.success) { const logs = result.logs.map((log) => log.message).join('\n') throw new Error(`[build-sidecars] Failed to compile ${productName}:\n${logs}`) } const outputPath = result.outputs[0]?.path console.log(`[build-sidecars] ${productName} -> ${outputPath ?? outfileBase}`) }