import { createWriteStream } from "fs"; import { chmodSync, existsSync, mkdirSync, statSync } from "fs"; import { pipeline } from "stream/promises"; import { fileURLToPath } from "url"; import path from "path"; import { Readable } from "stream"; import https from "https"; import { createGunzip } from "zlib"; import { execFileSync } from "child_process"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const binDir = path.join(__dirname, "..", "bin"); const binPath = path.join(binDir, "opencode"); const URL = "https://github.com/Anomalyco/opencode/releases/latest/download/opencode-linux-x64.tar.gz"; if (existsSync(binPath) && statSync(binPath).size > 1_000_000) { console.log("[fetch-opencode] binary already present, skipping"); process.exit(0); } mkdirSync(binDir, { recursive: true }); const tmp = path.join(binDir, "opencode.tar.gz"); const res = await fetch(URL); if (!res.ok) throw new Error(`download failed: ${res.status} ${res.statusText}`); await pipeline(Readable.fromWeb(res.body), createWriteStream(tmp)); console.log("[fetch-opencode] downloaded", statSync(tmp).size, "bytes"); // tar.gz contains a single file: opencode execFileSync("tar", ["-xzf", tmp, "-C", binDir], { stdio: "inherit" }); chmodSync(binPath, 0o755); console.log("[fetch-opencode] extracted + chmod 755 ->", binPath);