cloudaxe-opencode/packages/cli/script/build.ts

132 lines
4.2 KiB
TypeScript
Raw Permalink Normal View History

#!/usr/bin/env bun
import { $ } from "bun"
import { rm } from "fs/promises"
import path from "path"
import { Script } from "@opencode-ai/script"
import { createSolidTransformPlugin } from "@opentui/solid/bun-plugin"
2026-07-29 02:32:08 +00:00
import type { BunPlugin } from "bun"
import pkg from "../package.json"
import { modelsData } from "./generate"
const dir = path.resolve(import.meta.dirname, "..")
2026-06-26 18:20:47 +00:00
const binary = "opencode2"
2026-07-17 12:45:06 +00:00
const outdir = path.resolve(
dir,
process.argv.find((arg) => arg.startsWith("--outdir="))?.slice("--outdir=".length) ?? "dist",
)
if (outdir === dir) throw new Error("--outdir must not be the package directory")
process.chdir(dir)
2026-07-17 12:45:06 +00:00
await rm(outdir, { recursive: true, force: true })
const singleFlag = process.argv.includes("--single")
const baselineFlag = process.argv.includes("--baseline")
const skipInstall = process.argv.includes("--skip-install")
2026-07-29 02:32:08 +00:00
const solidPlugin = createSolidTransformPlugin()
const allTargets: {
os: string
arch: "arm64" | "x64"
abi?: "musl"
avx2?: false
}[] = [
{ os: "linux", arch: "arm64" },
{ os: "linux", arch: "x64" },
{ os: "linux", arch: "x64", avx2: false },
{ os: "linux", arch: "arm64", abi: "musl" },
{ os: "linux", arch: "x64", abi: "musl" },
{ os: "linux", arch: "x64", abi: "musl", avx2: false },
{ os: "darwin", arch: "arm64" },
{ os: "darwin", arch: "x64" },
{ os: "darwin", arch: "x64", avx2: false },
{ os: "win32", arch: "arm64" },
{ os: "win32", arch: "x64" },
{ os: "win32", arch: "x64", avx2: false },
]
const targets = singleFlag
? allTargets.filter((item) => {
if (item.os !== process.platform || item.arch !== process.arch) return false
if (item.avx2 === false) return baselineFlag
return item.abi === undefined
})
: allTargets
if (!skipInstall) await $`bun install --os="*" --cpu="*" @opentui/core@${pkg.dependencies["@opentui/core"]}`
for (const item of targets) {
2026-07-29 02:32:08 +00:00
const parcelWatcherPackage = `@parcel/watcher-${item.os}-${item.arch}${item.os === "linux" ? `-${item.abi ?? "glibc"}` : ""}`
const parcelWatcherPlugin: BunPlugin = {
name: "parcel-watcher-binding",
setup(build) {
build.onLoad({ filter: /filesystem\/watcher-binding\.ts$/ }, () => ({
contents: `import binding from ${JSON.stringify(parcelWatcherPackage)}; export default () => binding`,
loader: "js",
}))
},
}
2026-06-04 23:23:26 +00:00
const target = [
binary,
item.os === "win32" ? "windows" : item.os,
item.arch,
item.avx2 === false ? "baseline" : undefined,
item.abi,
]
.filter(Boolean)
.join("-")
2026-06-04 23:23:26 +00:00
const name = target.replace(binary, "cli")
console.log(`building ${name}`)
const result = await Bun.build({
2026-07-17 12:45:06 +00:00
entrypoints: ["./src/index.ts"],
tsconfig: "./tsconfig.json",
2026-07-29 02:32:08 +00:00
plugins: [solidPlugin, parcelWatcherPlugin],
external: ["node-gyp"],
format: "esm",
minify: true,
2026-07-06 23:22:12 +00:00
sourcemap: "inline",
splitting: true,
compile: {
autoloadBunfig: false,
autoloadDotenv: false,
autoloadTsconfig: true,
autoloadPackageJson: true,
2026-06-04 23:23:26 +00:00
target: target.replace(binary, "bun") as Bun.Build.CompileTarget,
2026-07-17 12:45:06 +00:00
outfile: path.join(outdir, name, "bin", binary),
execArgv: [`--user-agent=${binary}/${Script.version}`, "--use-system-ca", "--"],
windows: {},
},
define: {
OPENCODE_VERSION: `'${Script.version}'`,
OPENCODE_CLI_NAME: `'${binary}'`,
OPENCODE_MODELS_DEV: modelsData,
OPENCODE_CHANNEL: `'${Script.channel}'`,
OPENCODE_LIBC: item.os === "linux" ? `'${item.abi ?? "glibc"}'` : "undefined",
2026-06-10 02:28:34 +00:00
// FFF_LIBC selects the fff native lib variant: "musl" or "gnu".
FFF_LIBC: item.os === "linux" ? `'${item.abi ?? "gnu"}'` : "undefined",
...(item.os === "linux" ? { "process.env.OPENTUI_LIBC": JSON.stringify(item.abi ?? "glibc") } : {}),
},
})
if (!result.success) {
for (const log of result.logs) console.error(log)
process.exit(1)
}
await Bun.write(
2026-07-17 12:45:06 +00:00
path.join(outdir, name, "package.json"),
JSON.stringify(
{
name: `@opencode-ai/${name}`,
version: Script.version,
license: "MIT",
2026-06-04 23:23:26 +00:00
repository: { type: "git", url: "git+https://github.com/anomalyco/opencode.git" },
os: [item.os],
cpu: [item.arch],
},
null,
2,
),
)
}