File size: 1,331 Bytes
f5eeff1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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);