build.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 singleFlag = process.argv.includes("--single")
  53. const skipInstall = process.argv.includes("--skip-install")
  54. const allTargets: {
  55. os: string
  56. arch: "arm64" | "x64"
  57. abi?: "musl"
  58. avx2?: false
  59. }[] = [
  60. {
  61. os: "linux",
  62. arch: "arm64",
  63. },
  64. {
  65. os: "linux",
  66. arch: "x64",
  67. },
  68. {
  69. os: "darwin",
  70. arch: "arm64",
  71. },
  72. {
  73. os: "darwin",
  74. arch: "x64",
  75. },
  76. {
  77. os: "win32",
  78. arch: "x64",
  79. },
  80. ]
  81. const targets = singleFlag
  82. ? allTargets.filter((item) => item.os === process.platform && item.arch === process.arch)
  83. : allTargets
  84. await fs.rm("dist", { recursive: true, force: true })
  85. const binaries: Record<string, string> = {}
  86. if (!skipInstall) {
  87. await $`bun install --os="*" --cpu="*" @opentui/core@${pkg.dependencies["@opentui/core"]}`
  88. await $`bun install --os="*" --cpu="*" @parcel/watcher@${pkg.dependencies["@parcel/watcher"]}`
  89. }
  90. for (const item of targets) {
  91. const name = [
  92. pkg.name,
  93. // changing to win32 flags npm for some reason
  94. item.os === "win32" ? "windows" : item.os,
  95. item.arch,
  96. item.avx2 === false ? "baseline" : undefined,
  97. item.abi === undefined ? undefined : item.abi,
  98. ]
  99. .filter(Boolean)
  100. .join("-")
  101. console.log(`building ${name}`)
  102. await fs.mkdir(`dist/${name}/bin`, { recursive: true })
  103. const opentuiCoreEntry = require.resolve("@opentui/core")
  104. const parserWorker = nodefs.realpathSync(path.join(path.dirname(opentuiCoreEntry), "parser.worker.js"))
  105. const workerPath = "./src/cli/cmd/tui/worker.ts"
  106. // Use platform-specific bunfs root path based on target OS
  107. const bunfsRoot = item.os === "win32" ? "B:/~BUN/root/" : "/$bunfs/root/"
  108. const workerRelativePath = path.relative(dir, parserWorker).replaceAll("\\", "/")
  109. await Bun.build({
  110. conditions: ["browser"],
  111. tsconfig: "./tsconfig.json",
  112. plugins: [solidPlugin],
  113. sourcemap: "external",
  114. compile: {
  115. autoloadBunfig: false,
  116. autoloadDotenv: false,
  117. target: name.replace(pkg.name, "bun") as any,
  118. outfile: `dist/${name}/bin/opencode`,
  119. execArgv: [`--user-agent=opencode/${Script.version}`, "--"],
  120. windows: {},
  121. },
  122. entrypoints: ["./src/index.ts", parserWorker, workerPath],
  123. define: {
  124. OPENCODE_VERSION: `'${Script.version}'`,
  125. OTUI_TREE_SITTER_WORKER_PATH: bunfsRoot + workerRelativePath,
  126. OPENCODE_WORKER_PATH: workerPath,
  127. OPENCODE_CHANNEL: `'${Script.channel}'`,
  128. },
  129. })
  130. await fs.rm(`./dist/${name}/bin/tui`, { recursive: true, force: true })
  131. await Bun.file(`dist/${name}/package.json`).write(
  132. JSON.stringify(
  133. {
  134. name,
  135. version: Script.version,
  136. os: [item.os],
  137. cpu: [item.arch],
  138. },
  139. null,
  140. 2,
  141. ),
  142. )
  143. binaries[name] = Script.version
  144. }
  145. export { binaries }