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