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