build.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env bun
  2. const dir = new URL("..", import.meta.url).pathname
  3. process.chdir(dir)
  4. import { $ } from "bun"
  5. import pkg from "../package.json"
  6. const GOARCH: Record<string, string> = {
  7. arm64: "arm64",
  8. x64: "amd64",
  9. "x64-baseline": "amd64",
  10. }
  11. const targets = [
  12. ["windows", "x64"],
  13. ["linux", "arm64"],
  14. ["linux", "x64"],
  15. ["linux", "x64-baseline"],
  16. ["darwin", "x64"],
  17. ["darwin", "x64-baseline"],
  18. ["darwin", "arm64"],
  19. ]
  20. await $`rm -rf dist`
  21. const binaries: Record<string, string> = {}
  22. const version = process.env["OPENCODE_VERSION"] ?? "dev"
  23. for (const [os, arch] of targets) {
  24. console.log(`building ${os}-${arch}`)
  25. const name = `${pkg.name}-${os}-${arch}`
  26. await $`mkdir -p dist/${name}/bin`
  27. await $`CGO_ENABLED=0 GOOS=${os} GOARCH=${GOARCH[arch]} go build -ldflags="-s -w -X main.Version=${version}" -o ../opencode/dist/${name}/bin/tui ../tui/cmd/opencode/main.go`
  28. .cwd("../tui")
  29. .quiet()
  30. await Bun.build({
  31. compile: {
  32. target: `bun-${os}-${arch}` as any,
  33. outfile: `dist/${name}/bin/opencode`,
  34. execArgv: [`--user-agent=opencode/${version}`, `--env-file=""`, `--`],
  35. windows: {},
  36. },
  37. entrypoints: ["./src/index.ts"],
  38. define: {
  39. OPENCODE_VERSION: `'${version}'`,
  40. OPENCODE_TUI_PATH: `'../../../dist/${name}/bin/tui'`,
  41. },
  42. })
  43. await $`rm -rf ./dist/${name}/bin/tui`
  44. await Bun.file(`dist/${name}/package.json`).write(
  45. JSON.stringify(
  46. {
  47. name,
  48. version,
  49. os: [os === "windows" ? "win32" : os],
  50. cpu: [arch],
  51. },
  52. null,
  53. 2,
  54. ),
  55. )
  56. binaries[name] = version
  57. }
  58. export { binaries }