build.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. const GOARCH: Record<string, string> = {
  8. arm64: "arm64",
  9. x64: "amd64",
  10. "x64-baseline": "amd64",
  11. }
  12. const targets = [
  13. ["windows", "x64"],
  14. ["linux", "arm64"],
  15. ["linux", "x64"],
  16. ["linux", "x64-baseline"],
  17. ["darwin", "x64"],
  18. ["darwin", "x64-baseline"],
  19. ["darwin", "arm64"],
  20. ]
  21. await $`rm -rf dist`
  22. const binaries: Record<string, string> = {}
  23. const version = process.env["OPENCODE_VERSION"] ?? "dev"
  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=${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. compile: {
  37. target: `bun-${os}-${arch}` as any,
  38. outfile: `dist/${name}/bin/opencode`,
  39. execArgv: [`--user-agent=opencode/${version}`, `--env-file=""`, `--`],
  40. windows: {},
  41. },
  42. entrypoints: ["./src/index.ts"],
  43. define: {
  44. OPENCODE_VERSION: `'${version}'`,
  45. OPENCODE_TUI_PATH: `'../../../dist/${name}/bin/tui'`,
  46. },
  47. })
  48. await $`rm -rf ./dist/${name}/bin/tui`
  49. await Bun.file(`dist/${name}/package.json`).write(
  50. JSON.stringify(
  51. {
  52. name,
  53. version,
  54. os: [os === "windows" ? "win32" : os],
  55. cpu: [arch],
  56. },
  57. null,
  58. 2,
  59. ),
  60. )
  61. binaries[name] = version
  62. }
  63. export { binaries }