build.ts 4.2 KB

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