Repository navigation
Expand file tree
/
Copy pathBuild.ts
More file actions
42 lines (38 loc) · 1.71 KB
/
Copy pathBuild.ts
File metadata and controls
42 lines (38 loc) · 1.71 KB
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
33
34
35
36
37
38
39
40
41
42
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
const HERE = import.meta.dir;
const SDK = HERE + "/node_modules/@anthropic-ai/claude-agent-sdk";
const here = (process.platform === "win32" ? "windows" : process.platform) + "-" + process.arch;
const target = process.argv[2] ?? "bun-" + here;
const parts = target.replace(/^bun-/, "").split("-");
const win = parts[0] === "windows";
const pkg = "@anthropic-ai/claude-agent-sdk-" + [win ? "win32" : parts[0], ...parts.slice(1)].join("-");
const exe = HERE + "/node_modules/" + pkg + (win ? "/claude.exe" : "/claude");
const out = process.argv[3] ?? (win ? "unii.exe" : "unii");
function run(cmd: string[], cwd = HERE): void {
const p = Bun.spawnSync(cmd, { cwd, stdio: ["inherit", "inherit", "inherit"] });
if (p.exitCode !== 0) {
throw new Error(cmd.join(" ") + ": exit " + p.exitCode);
}
}
if (!fs.existsSync(SDK)) {
throw new Error("run `bun install` first");
}
if (!fs.existsSync(exe)) {
const { version } = JSON.parse(fs.readFileSync(SDK + "/package.json", "utf8")) as { version: string };
const tmp = fs.mkdtempSync(os.tmpdir() + "/unii-build-");
try {
run(["npm", "pack", pkg + "@" + version, "--pack-destination", tmp], tmp);
const tgz = fs.readdirSync(tmp).find((f) => f.endsWith(".tgz"));
if (tgz === undefined) {
throw new Error("npm pack made no .tgz");
}
const dir = path.dirname(exe);
fs.mkdirSync(dir, { recursive: true });
run(["tar", "-xzf", path.join(tmp, tgz), "-C", dir, "--strip-components=1"]);
} finally {
fs.rmSync(tmp, { recursive: true, force: true });
}
}
run(["bun", "build", "--compile", "--target=" + target, "Cli.ts", exe, "--outfile", out]);