build.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 modelsUrl = process.env.OPENCODE_MODELS_URL || "https://models.dev"
  14. // Fetch and generate models.dev snapshot
  15. const modelsData = process.env.MODELS_DEV_API_JSON
  16. ? await Bun.file(process.env.MODELS_DEV_API_JSON).text()
  17. : await fetch(`${modelsUrl}/api.json`).then((x) => x.text())
  18. await Bun.write(
  19. path.join(dir, "src/provider/models-snapshot.ts"),
  20. `// Auto-generated by build.ts - do not edit\nexport const snapshot = ${modelsData} as const\n`,
  21. )
  22. console.log("Generated models-snapshot.ts")
  23. const singleFlag = process.argv.includes("--single")
  24. const baselineFlag = process.argv.includes("--baseline")
  25. const skipInstall = process.argv.includes("--skip-install")
  26. const allTargets: {
  27. os: string
  28. arch: "arm64" | "x64"
  29. abi?: "musl"
  30. avx2?: false
  31. }[] = [
  32. {
  33. os: "linux",
  34. arch: "arm64",
  35. },
  36. {
  37. os: "linux",
  38. arch: "x64",
  39. },
  40. {
  41. os: "linux",
  42. arch: "x64",
  43. avx2: false,
  44. },
  45. {
  46. os: "linux",
  47. arch: "arm64",
  48. abi: "musl",
  49. },
  50. {
  51. os: "linux",
  52. arch: "x64",
  53. abi: "musl",
  54. },
  55. {
  56. os: "linux",
  57. arch: "x64",
  58. abi: "musl",
  59. avx2: false,
  60. },
  61. {
  62. os: "darwin",
  63. arch: "arm64",
  64. },
  65. {
  66. os: "darwin",
  67. arch: "x64",
  68. },
  69. {
  70. os: "darwin",
  71. arch: "x64",
  72. avx2: false,
  73. },
  74. {
  75. os: "win32",
  76. arch: "x64",
  77. },
  78. {
  79. os: "win32",
  80. arch: "x64",
  81. avx2: false,
  82. },
  83. ]
  84. const targets = singleFlag
  85. ? allTargets.filter((item) => {
  86. if (item.os !== process.platform || item.arch !== process.arch) {
  87. return false
  88. }
  89. // When building for the current platform, prefer a single native binary by default.
  90. // Baseline binaries require additional Bun artifacts and can be flaky to download.
  91. if (item.avx2 === false) {
  92. return baselineFlag
  93. }
  94. // also skip abi-specific builds for the same reason
  95. if (item.abi !== undefined) {
  96. return false
  97. }
  98. return true
  99. })
  100. : allTargets
  101. await $`rm -rf dist`
  102. const binaries: Record<string, string> = {}
  103. if (!skipInstall) {
  104. await $`bun install --os="*" --cpu="*" @opentui/core@${pkg.dependencies["@opentui/core"]}`
  105. await $`bun install --os="*" --cpu="*" @parcel/watcher@${pkg.dependencies["@parcel/watcher"]}`
  106. }
  107. for (const item of targets) {
  108. const name = [
  109. pkg.name,
  110. // changing to win32 flags npm for some reason
  111. item.os === "win32" ? "windows" : item.os,
  112. item.arch,
  113. item.avx2 === false ? "baseline" : undefined,
  114. item.abi === undefined ? undefined : item.abi,
  115. ]
  116. .filter(Boolean)
  117. .join("-")
  118. console.log(`building ${name}`)
  119. await $`mkdir -p dist/${name}/bin`
  120. const parserWorker = fs.realpathSync(path.resolve(dir, "./node_modules/@opentui/core/parser.worker.js"))
  121. const workerPath = "./src/cli/cmd/tui/worker.ts"
  122. // Use platform-specific bunfs root path based on target OS
  123. const bunfsRoot = item.os === "win32" ? "B:/~BUN/root/" : "/$bunfs/root/"
  124. const workerRelativePath = path.relative(dir, parserWorker).replaceAll("\\", "/")
  125. await Bun.build({
  126. conditions: ["browser"],
  127. tsconfig: "./tsconfig.json",
  128. plugins: [solidPlugin],
  129. sourcemap: "external",
  130. compile: {
  131. autoloadBunfig: false,
  132. autoloadDotenv: false,
  133. //@ts-ignore (bun types aren't up to date)
  134. autoloadTsconfig: true,
  135. autoloadPackageJson: true,
  136. target: name.replace(pkg.name, "bun") as any,
  137. outfile: `dist/${name}/bin/opencode`,
  138. execArgv: [`--user-agent=opencode/${Script.version}`, "--use-system-ca", "--"],
  139. windows: {},
  140. },
  141. entrypoints: ["./src/index.ts", parserWorker, workerPath],
  142. define: {
  143. OPENCODE_VERSION: `'${Script.version}'`,
  144. OTUI_TREE_SITTER_WORKER_PATH: bunfsRoot + workerRelativePath,
  145. OPENCODE_WORKER_PATH: workerPath,
  146. OPENCODE_CHANNEL: `'${Script.channel}'`,
  147. OPENCODE_LIBC: item.os === "linux" ? `'${item.abi ?? "glibc"}'` : "",
  148. },
  149. })
  150. await $`rm -rf ./dist/${name}/bin/tui`
  151. await Bun.file(`dist/${name}/package.json`).write(
  152. JSON.stringify(
  153. {
  154. name,
  155. version: Script.version,
  156. os: [item.os],
  157. cpu: [item.arch],
  158. },
  159. null,
  160. 2,
  161. ),
  162. )
  163. binaries[name] = Script.version
  164. }
  165. if (Script.release) {
  166. for (const key of Object.keys(binaries)) {
  167. if (key.includes("linux")) {
  168. await $`tar -czf ../../${key}.tar.gz *`.cwd(`dist/${key}/bin`)
  169. } else {
  170. await $`zip -r ../../${key}.zip *`.cwd(`dist/${key}/bin`)
  171. }
  172. }
  173. await $`gh release upload v${Script.version} ./dist/*.zip ./dist/*.tar.gz --clobber`
  174. }
  175. export { binaries }