build.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env bun
  2. import path from "path"
  3. import { fileURLToPath } from "url"
  4. const __filename = fileURLToPath(import.meta.url)
  5. const __dirname = path.dirname(__filename)
  6. const dir = path.resolve(__dirname, "..")
  7. process.chdir(dir)
  8. import { $ } from "bun"
  9. import pkg from "../package.json"
  10. import { Script } from "@opencode-ai/script"
  11. const GOARCH: Record<string, string> = {
  12. arm64: "arm64",
  13. x64: "amd64",
  14. "x64-baseline": "amd64",
  15. }
  16. const targets = [
  17. ["windows", "x64"],
  18. ["linux", "arm64"],
  19. ["linux", "x64"],
  20. ["linux", "x64-baseline"],
  21. ["darwin", "x64"],
  22. ["darwin", "x64-baseline"],
  23. ["darwin", "arm64"],
  24. ]
  25. await $`rm -rf dist`
  26. const binaries: Record<string, string> = {}
  27. for (const [os, arch] of targets) {
  28. console.log(`building ${os}-${arch}`)
  29. const name = `${pkg.name}-${os}-${arch}`
  30. await $`mkdir -p dist/${name}/bin`
  31. await $`CGO_ENABLED=0 GOOS=${os} GOARCH=${GOARCH[arch]} go build -ldflags="-s -w -X main.Version=${Script.version}" -o ../opencode/dist/${name}/bin/tui ../tui/cmd/opencode/main.go`
  32. .cwd("../tui")
  33. .quiet()
  34. const watcher = `@parcel/watcher-${os === "windows" ? "win32" : os}-${arch.replace("-baseline", "")}${os === "linux" ? "-glibc" : ""}`
  35. await $`mkdir -p ../../node_modules/${watcher}`
  36. await $`npm pack npm pack ${watcher}`.cwd(path.join(dir, "../../node_modules")).quiet()
  37. await $`tar -xf ../../node_modules/${watcher.replace("@parcel/", "parcel-")}-*.tgz -C ../../node_modules/${watcher} --strip-components=1`
  38. await Bun.build({
  39. sourcemap: "external",
  40. compile: {
  41. target: `bun-${os}-${arch}` as any,
  42. outfile: `dist/${name}/bin/opencode`,
  43. execArgv: [`--user-agent=opencode/${Script.version}`, `--env-file=""`, `--`],
  44. windows: {},
  45. },
  46. entrypoints: ["./src/index.ts"],
  47. define: {
  48. OPENCODE_VERSION: `'${Script.version}'`,
  49. OPENCODE_CHANNEL: `'${Script.channel}'`,
  50. OPENCODE_TUI_PATH: `'../../../dist/${name}/bin/tui'`,
  51. },
  52. })
  53. await $`rm -rf ./dist/${name}/bin/tui`
  54. await Bun.file(`dist/${name}/package.json`).write(
  55. JSON.stringify(
  56. {
  57. name,
  58. version: Script.version,
  59. os: [os === "windows" ? "win32" : os],
  60. cpu: [arch],
  61. },
  62. null,
  63. 2,
  64. ),
  65. )
  66. binaries[name] = Script.version
  67. }
  68. export { binaries }