build.ts 2.1 KB

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