build.ts 4.4 KB

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