build.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #!/usr/bin/env bun
  2. import solidPlugin from "./solid-plugin"
  3. import path from "path"
  4. import { $ } from "bun"
  5. import { fileURLToPath } from "url"
  6. import { createRequire } from "module"
  7. const __filename = fileURLToPath(import.meta.url)
  8. const __dirname = path.dirname(__filename)
  9. const require = createRequire(import.meta.url)
  10. const dir = path.resolve(__dirname, "..")
  11. process.chdir(dir)
  12. import pkg from "../package.json"
  13. import { Script } from "@opencode-ai/script"
  14. import fs from "fs/promises"
  15. import nodefs from "fs"
  16. await $`bun run build:webgui`
  17. const webGuiDir = path.join(dir, "webgui-dist")
  18. const embedOutput = path.join(dir, "src/webgui/embed.generated.ts")
  19. async function listWebGuiFiles(current: string) {
  20. const entries = await fs.readdir(current, { withFileTypes: true })
  21. const files: { path: string; data: string }[] = []
  22. for (const entry of entries) {
  23. const absolute = path.join(current, entry.name)
  24. if (entry.isDirectory()) {
  25. const nested = await listWebGuiFiles(absolute)
  26. files.push(...nested)
  27. continue
  28. }
  29. const relative = path.relative(webGuiDir, absolute).split(path.sep).join("/")
  30. const file = Bun.file(absolute)
  31. const buffer = Buffer.from(await file.arrayBuffer())
  32. files.push({ path: relative, data: buffer.toString("base64") })
  33. }
  34. return files
  35. }
  36. async function generateEmbeddedWebGui() {
  37. const indexFile = Bun.file(path.join(webGuiDir, "index.html"))
  38. if (!(await indexFile.exists())) {
  39. await Bun.write(embedOutput, "export const embeddedWebGui = [] as const\n")
  40. return
  41. }
  42. const items = await listWebGuiFiles(webGuiDir)
  43. const lines = [
  44. "export const embeddedWebGui = [",
  45. ...items.map((item) => ` { path: ${JSON.stringify(item.path)}, data: ${JSON.stringify(item.data)} },`),
  46. "] as const",
  47. "",
  48. ]
  49. await Bun.write(embedOutput, lines.join("\n"))
  50. }
  51. await generateEmbeddedWebGui()
  52. const modelsUrl = process.env.OPENCODE_MODELS_URL || "https://models.dev"
  53. // Fetch and generate models.dev snapshot
  54. const modelsData = process.env.MODELS_DEV_API_JSON
  55. ? await Bun.file(process.env.MODELS_DEV_API_JSON).text()
  56. : await fetch(`${modelsUrl}/api.json`).then((x) => x.text())
  57. await Bun.write(
  58. path.join(dir, "src/provider/models-snapshot.ts"),
  59. `// Auto-generated by build.ts - do not edit\nexport const snapshot = ${modelsData} as const\n`,
  60. )
  61. console.log("Generated models-snapshot.ts")
  62. // Load migrations from migration directories
  63. const migrationDirs = (await fs.readdir(path.join(dir, "migration"), { withFileTypes: true }))
  64. .filter((entry) => entry.isDirectory() && /^\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}/.test(entry.name))
  65. .map((entry) => entry.name)
  66. .sort()
  67. const migrations = await Promise.all(
  68. migrationDirs.map(async (name) => {
  69. const file = path.join(dir, "migration", name, "migration.sql")
  70. const sql = await Bun.file(file).text()
  71. const match = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/.exec(name)
  72. const timestamp = match
  73. ? Date.UTC(
  74. Number(match[1]),
  75. Number(match[2]) - 1,
  76. Number(match[3]),
  77. Number(match[4]),
  78. Number(match[5]),
  79. Number(match[6]),
  80. )
  81. : 0
  82. return { sql, timestamp }
  83. }),
  84. )
  85. console.log(`Loaded ${migrations.length} migrations`)
  86. const singleFlag = process.argv.includes("--single")
  87. const baselineFlag = process.argv.includes("--baseline")
  88. const skipInstall = process.argv.includes("--skip-install")
  89. const allTargets: {
  90. os: string
  91. arch: "arm64" | "x64"
  92. abi?: "musl"
  93. avx2?: false
  94. }[] = [
  95. {
  96. os: "linux",
  97. arch: "arm64",
  98. },
  99. {
  100. os: "linux",
  101. arch: "x64",
  102. },
  103. {
  104. os: "darwin",
  105. arch: "arm64",
  106. },
  107. {
  108. os: "darwin",
  109. arch: "x64",
  110. },
  111. {
  112. os: "win32",
  113. arch: "x64",
  114. },
  115. ]
  116. const targets = singleFlag
  117. ? allTargets.filter((item) => {
  118. if (item.os !== process.platform || item.arch !== process.arch) {
  119. return false
  120. }
  121. // When building for the current platform, prefer a single native binary by default.
  122. // Baseline binaries require additional Bun artifacts and can be flaky to download.
  123. if (item.avx2 === false) {
  124. return baselineFlag
  125. }
  126. // also skip abi-specific builds for the same reason
  127. if (item.abi !== undefined) {
  128. return false
  129. }
  130. return true
  131. })
  132. : allTargets
  133. await fs.rm("dist", { recursive: true, force: true })
  134. const binaries: Record<string, string> = {}
  135. if (!skipInstall) {
  136. await $`bun install --os="*" --cpu="*" @opentui/core@${pkg.dependencies["@opentui/core"]}`
  137. await $`bun install --os="*" --cpu="*" @parcel/watcher@${pkg.dependencies["@parcel/watcher"]}`
  138. }
  139. for (const item of targets) {
  140. const name = [
  141. pkg.name,
  142. // changing to win32 flags npm for some reason
  143. item.os === "win32" ? "windows" : item.os,
  144. item.arch,
  145. item.avx2 === false ? "baseline" : undefined,
  146. item.abi === undefined ? undefined : item.abi,
  147. ]
  148. .filter(Boolean)
  149. .join("-")
  150. console.log(`building ${name}`)
  151. await fs.mkdir(`dist/${name}/bin`, { recursive: true })
  152. const opentuiCoreEntry = require.resolve("@opentui/core")
  153. const parserWorker = nodefs.realpathSync(path.join(path.dirname(opentuiCoreEntry), "parser.worker.js"))
  154. const workerPath = "./src/cli/cmd/tui/worker.ts"
  155. // Use platform-specific bunfs root path based on target OS
  156. const bunfsRoot = item.os === "win32" ? "B:/~BUN/root/" : "/$bunfs/root/"
  157. const workerRelativePath = path.relative(dir, parserWorker).replaceAll("\\", "/")
  158. await Bun.build({
  159. conditions: ["browser"],
  160. tsconfig: "./tsconfig.json",
  161. plugins: [solidPlugin],
  162. sourcemap: "external",
  163. compile: {
  164. autoloadBunfig: false,
  165. autoloadDotenv: false,
  166. //@ts-ignore (bun types aren't up to date)
  167. autoloadTsconfig: true,
  168. autoloadPackageJson: true,
  169. target: name.replace(pkg.name, "bun") as any,
  170. outfile: `dist/${name}/bin/opencode`,
  171. execArgv: [`--user-agent=opencode/${Script.version}`, "--use-system-ca", "--"],
  172. windows: {},
  173. },
  174. entrypoints: ["./src/index.ts", parserWorker, workerPath],
  175. define: {
  176. OPENCODE_VERSION: `'${Script.version}'`,
  177. OPENCODE_MIGRATIONS: JSON.stringify(migrations),
  178. OTUI_TREE_SITTER_WORKER_PATH: bunfsRoot + workerRelativePath,
  179. OPENCODE_WORKER_PATH: workerPath,
  180. OPENCODE_CHANNEL: `'${Script.channel}'`,
  181. OPENCODE_LIBC: item.os === "linux" ? `'${item.abi ?? "glibc"}'` : "",
  182. },
  183. })
  184. await fs.rm(`./dist/${name}/bin/tui`, { recursive: true, force: true })
  185. await Bun.file(`dist/${name}/package.json`).write(
  186. JSON.stringify(
  187. {
  188. name,
  189. version: Script.version,
  190. os: [item.os],
  191. cpu: [item.arch],
  192. },
  193. null,
  194. 2,
  195. ),
  196. )
  197. binaries[name] = Script.version
  198. }
  199. if (Script.release) {
  200. for (const key of Object.keys(binaries)) {
  201. if (key.includes("linux")) {
  202. await $`tar -czf ../../${key}.tar.gz *`.cwd(`dist/${key}/bin`)
  203. } else {
  204. await $`zip -r ../../${key}.zip *`.cwd(`dist/${key}/bin`)
  205. }
  206. }
  207. await $`gh release upload v${Script.version} ./dist/*.zip ./dist/*.tar.gz --clobber`
  208. }
  209. export { binaries }