build.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. return true
  85. })
  86. : allTargets
  87. await $`rm -rf dist`
  88. const binaries: Record<string, string> = {}
  89. if (!skipInstall) {
  90. await $`bun install --os="*" --cpu="*" @opentui/core@${pkg.dependencies["@opentui/core"]}`
  91. await $`bun install --os="*" --cpu="*" @parcel/watcher@${pkg.dependencies["@parcel/watcher"]}`
  92. }
  93. for (const item of targets) {
  94. const name = [
  95. pkg.name,
  96. // changing to win32 flags npm for some reason
  97. item.os === "win32" ? "windows" : item.os,
  98. item.arch,
  99. item.avx2 === false ? "baseline" : undefined,
  100. item.abi === undefined ? undefined : item.abi,
  101. ]
  102. .filter(Boolean)
  103. .join("-")
  104. console.log(`building ${name}`)
  105. await $`mkdir -p dist/${name}/bin`
  106. const parserWorker = fs.realpathSync(path.resolve(dir, "./node_modules/@opentui/core/parser.worker.js"))
  107. const workerPath = "./src/cli/cmd/tui/worker.ts"
  108. // Use platform-specific bunfs root path based on target OS
  109. const bunfsRoot = item.os === "win32" ? "B:/~BUN/root/" : "/$bunfs/root/"
  110. const workerRelativePath = path.relative(dir, parserWorker).replaceAll("\\", "/")
  111. await Bun.build({
  112. conditions: ["browser"],
  113. tsconfig: "./tsconfig.json",
  114. plugins: [solidPlugin],
  115. sourcemap: "external",
  116. compile: {
  117. autoloadBunfig: false,
  118. autoloadDotenv: false,
  119. //@ts-ignore (bun types aren't up to date)
  120. autoloadTsconfig: true,
  121. autoloadPackageJson: true,
  122. target: name.replace(pkg.name, "bun") as any,
  123. outfile: `dist/${name}/bin/opencode`,
  124. execArgv: [`--user-agent=opencode/${Script.version}`, "--use-system-ca", "--"],
  125. windows: {},
  126. },
  127. entrypoints: ["./src/index.ts", parserWorker, workerPath],
  128. define: {
  129. OPENCODE_VERSION: `'${Script.version}'`,
  130. OTUI_TREE_SITTER_WORKER_PATH: bunfsRoot + workerRelativePath,
  131. OPENCODE_WORKER_PATH: workerPath,
  132. OPENCODE_CHANNEL: `'${Script.channel}'`,
  133. OPENCODE_LIBC: item.os === "linux" ? `'${item.abi ?? "glibc"}'` : "",
  134. },
  135. })
  136. await $`rm -rf ./dist/${name}/bin/tui`
  137. await Bun.file(`dist/${name}/package.json`).write(
  138. JSON.stringify(
  139. {
  140. name,
  141. version: Script.version,
  142. os: [item.os],
  143. cpu: [item.arch],
  144. },
  145. null,
  146. 2,
  147. ),
  148. )
  149. binaries[name] = Script.version
  150. }
  151. export { binaries }