build.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/env bun
  2. import solidPlugin from "../node_modules/@opentui/solid/scripts/solid-plugin"
  3. import path from "path"
  4. import fs from "fs"
  5. import { $ } from "bun"
  6. import { fileURLToPath } from "url"
  7. const __filename = fileURLToPath(import.meta.url)
  8. const __dirname = path.dirname(__filename)
  9. const dir = path.resolve(__dirname, "..")
  10. process.chdir(dir)
  11. import pkg from "../package.json"
  12. import { Script } from "@opencode-ai/script"
  13. const singleFlag = process.argv.includes("--single")
  14. const baselineFlag = process.argv.includes("--baseline")
  15. const skipInstall = process.argv.includes("--skip-install")
  16. const allTargets: {
  17. os: string
  18. arch: "arm64" | "x64"
  19. abi?: "musl"
  20. avx2?: false
  21. }[] = [
  22. {
  23. os: "linux",
  24. arch: "arm64",
  25. },
  26. {
  27. os: "linux",
  28. arch: "x64",
  29. },
  30. {
  31. os: "linux",
  32. arch: "x64",
  33. avx2: false,
  34. },
  35. {
  36. os: "linux",
  37. arch: "arm64",
  38. abi: "musl",
  39. },
  40. {
  41. os: "linux",
  42. arch: "x64",
  43. abi: "musl",
  44. },
  45. {
  46. os: "linux",
  47. arch: "x64",
  48. abi: "musl",
  49. avx2: false,
  50. },
  51. {
  52. os: "darwin",
  53. arch: "arm64",
  54. },
  55. {
  56. os: "darwin",
  57. arch: "x64",
  58. },
  59. {
  60. os: "darwin",
  61. arch: "x64",
  62. avx2: false,
  63. },
  64. {
  65. os: "win32",
  66. arch: "x64",
  67. },
  68. {
  69. os: "win32",
  70. arch: "x64",
  71. avx2: false,
  72. },
  73. ]
  74. const targets = singleFlag
  75. ? allTargets.filter((item) => {
  76. if (item.os !== process.platform || item.arch !== process.arch) {
  77. return false
  78. }
  79. // When building for the current platform, prefer a single native binary by default.
  80. // Baseline binaries require additional Bun artifacts and can be flaky to download.
  81. if (item.avx2 === false) {
  82. return baselineFlag
  83. }
  84. // also skip abi-specific builds for the same reason
  85. if (item.abi !== undefined) {
  86. return false
  87. }
  88. return true
  89. })
  90. : allTargets
  91. await $`rm -rf dist`
  92. const binaries: Record<string, string> = {}
  93. if (!skipInstall) {
  94. await $`bun install --os="*" --cpu="*" @opentui/core@${pkg.dependencies["@opentui/core"]}`
  95. await $`bun install --os="*" --cpu="*" @parcel/watcher@${pkg.dependencies["@parcel/watcher"]}`
  96. }
  97. for (const item of targets) {
  98. const name = [
  99. pkg.name,
  100. // changing to win32 flags npm for some reason
  101. item.os === "win32" ? "windows" : item.os,
  102. item.arch,
  103. item.avx2 === false ? "baseline" : undefined,
  104. item.abi === undefined ? undefined : item.abi,
  105. ]
  106. .filter(Boolean)
  107. .join("-")
  108. console.log(`building ${name}`)
  109. await $`mkdir -p dist/${name}/bin`
  110. const parserWorker = fs.realpathSync(path.resolve(dir, "./node_modules/@opentui/core/parser.worker.js"))
  111. const workerPath = "./src/cli/cmd/tui/worker.ts"
  112. // Use platform-specific bunfs root path based on target OS
  113. const bunfsRoot = item.os === "win32" ? "B:/~BUN/root/" : "/$bunfs/root/"
  114. const workerRelativePath = path.relative(dir, parserWorker).replaceAll("\\", "/")
  115. await Bun.build({
  116. conditions: ["browser"],
  117. tsconfig: "./tsconfig.json",
  118. plugins: [solidPlugin],
  119. sourcemap: "external",
  120. compile: {
  121. autoloadBunfig: false,
  122. autoloadDotenv: false,
  123. //@ts-ignore (bun types aren't up to date)
  124. autoloadTsconfig: true,
  125. autoloadPackageJson: true,
  126. target: name.replace(pkg.name, "bun") as any,
  127. outfile: `dist/${name}/bin/opencode`,
  128. execArgv: [`--user-agent=opencode/${Script.version}`, "--use-system-ca", "--"],
  129. windows: {},
  130. },
  131. entrypoints: ["./src/index.ts", parserWorker, workerPath],
  132. define: {
  133. OPENCODE_VERSION: `'${Script.version}'`,
  134. OTUI_TREE_SITTER_WORKER_PATH: bunfsRoot + workerRelativePath,
  135. OPENCODE_WORKER_PATH: workerPath,
  136. OPENCODE_CHANNEL: `'${Script.channel}'`,
  137. OPENCODE_LIBC: item.os === "linux" ? `'${item.abi ?? "glibc"}'` : "",
  138. },
  139. })
  140. await $`rm -rf ./dist/${name}/bin/tui`
  141. await Bun.file(`dist/${name}/package.json`).write(
  142. JSON.stringify(
  143. {
  144. name,
  145. version: Script.version,
  146. os: [item.os],
  147. cpu: [item.arch],
  148. },
  149. null,
  150. 2,
  151. ),
  152. )
  153. binaries[name] = Script.version
  154. }
  155. export { binaries }