|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Build script for Claude CLI binary |
| 4 | + * |
| 5 | + * This script patches the Claude Code CLI to embed yoga.wasm as base64, |
| 6 | + * then compiles it with Bun to create a standalone binary. |
| 7 | + */ |
| 8 | + |
| 9 | +import { readFileSync, writeFileSync, mkdirSync } from 'fs'; |
| 10 | +import { execSync } from 'child_process'; |
| 11 | +import { dirname, join } from 'path'; |
| 12 | +import { fileURLToPath } from 'url'; |
| 13 | + |
| 14 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 15 | +const agentBridgeDir = dirname(__dirname); |
| 16 | +const binariesDir = join(agentBridgeDir, '..', 'src-tauri', 'binaries'); |
| 17 | + |
| 18 | +// Ensure binaries directory exists |
| 19 | +mkdirSync(binariesDir, { recursive: true }); |
| 20 | + |
| 21 | +console.log('📦 Building Claude CLI binary with embedded yoga.wasm...'); |
| 22 | + |
| 23 | +// Read the original CLI |
| 24 | +const cliPath = join(agentBridgeDir, 'node_modules', '@anthropic-ai', 'claude-code', 'cli.js'); |
| 25 | +const wasmPath = join(agentBridgeDir, 'node_modules', '@anthropic-ai', 'claude-code', 'yoga.wasm'); |
| 26 | + |
| 27 | +console.log(' Reading CLI source...'); |
| 28 | +const cli = readFileSync(cliPath, 'utf8'); |
| 29 | + |
| 30 | +// Read yoga.wasm and convert to base64 |
| 31 | +console.log(' Embedding yoga.wasm as base64...'); |
| 32 | +const wasmBuffer = readFileSync(wasmPath); |
| 33 | +const wasmBase64 = wasmBuffer.toString('base64'); |
| 34 | + |
| 35 | +// Replace the yoga.wasm loading pattern |
| 36 | +// Original: await idQ(ndQ(import.meta.url).resolve("./yoga.wasm")) |
| 37 | +// New: Buffer.from("base64data","base64") |
| 38 | +const patched = cli.replace( |
| 39 | + 'await idQ(ndQ(import.meta.url).resolve("./yoga.wasm"))', |
| 40 | + `Buffer.from("${wasmBase64}","base64")` |
| 41 | +); |
| 42 | + |
| 43 | +if (patched === cli) { |
| 44 | + console.error('❌ Failed to patch yoga.wasm loading - pattern not found'); |
| 45 | + process.exit(1); |
| 46 | +} |
| 47 | + |
| 48 | +// Write patched file to temp location |
| 49 | +const patchedPath = '/tmp/claude-cli-patched.js'; |
| 50 | +writeFileSync(patchedPath, patched); |
| 51 | +console.log(` Patched CLI size: ${patched.length} bytes (was ${cli.length})`); |
| 52 | + |
| 53 | +// Compile with Bun |
| 54 | +const outputPath = join(binariesDir, 'claude-aarch64-apple-darwin'); |
| 55 | +console.log(' Compiling with Bun...'); |
| 56 | + |
| 57 | +try { |
| 58 | + execSync(`bun build --compile --minify --target=bun-darwin-arm64 ${patchedPath} --outfile ${outputPath}`, { |
| 59 | + stdio: 'inherit', |
| 60 | + cwd: agentBridgeDir |
| 61 | + }); |
| 62 | + console.log('✅ Claude CLI binary built successfully'); |
| 63 | +} catch (error) { |
| 64 | + console.error('❌ Failed to compile Claude CLI:', error.message); |
| 65 | + process.exit(1); |
| 66 | +} |
0 commit comments