build.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. const singleFlag = process.argv.includes("--single")
  14. const allTargets = [
  15. ["windows", "x64"],
  16. ["linux", "arm64"],
  17. ["linux", "x64"],
  18. ["linux", "x64-baseline"],
  19. ["darwin", "x64"],
  20. ["darwin", "x64-baseline"],
  21. ["darwin", "arm64"],
  22. ]
  23. const targets = singleFlag
  24. ? allTargets.filter(([os, arch]) => os === process.platform && arch === process.arch)
  25. : allTargets
  26. await $`rm -rf dist`
  27. const binaries: Record<string, string> = {}
  28. for (const [os, arch] of targets) {
  29. console.log(`building ${os}-${arch}`)
  30. const name = `${pkg.name}-${os}-${arch}`
  31. await $`mkdir -p dist/${name}/bin`
  32. const opentui = `@opentui/core-${os === "windows" ? "win32" : os}-${arch.replace("-baseline", "")}`
  33. await $`mkdir -p ../../node_modules/${opentui}`
  34. await $`npm pack ${opentui}@${pkg.dependencies["@opentui/core"]}`.cwd(path.join(dir, "../../node_modules"))
  35. await $`tar -xf ../../node_modules/${opentui.replace("@opentui/", "opentui-")}-*.tgz -C ../../node_modules/${opentui} --strip-components=1`
  36. const watcher = `@parcel/watcher-${os === "windows" ? "win32" : os}-${arch.replace("-baseline", "")}${os === "linux" ? "-glibc" : ""}`
  37. await $`mkdir -p ../../node_modules/${watcher}`
  38. await $`npm pack ${watcher}`.cwd(path.join(dir, "../../node_modules")).quiet()
  39. await $`tar -xf ../../node_modules/${watcher.replace("@parcel/", "parcel-")}-*.tgz -C ../../node_modules/${watcher} --strip-components=1`
  40. const parserWorker = fs.realpathSync(path.resolve(dir, "./node_modules/@opentui/core/parser.worker.js"))
  41. const workerPath = "./src/cli/cmd/tui/worker.ts"
  42. await Bun.build({
  43. conditions: ["browser"],
  44. tsconfig: "./tsconfig.json",
  45. plugins: [solidPlugin],
  46. sourcemap: "external",
  47. compile: {
  48. target: `bun-${os}-${arch}` as any,
  49. outfile: `dist/${name}/bin/opencode`,
  50. execArgv: [`--user-agent=opencode/${Script.version}`, `--env-file=""`, `--`],
  51. windows: {},
  52. },
  53. entrypoints: ["./src/index.ts", parserWorker, workerPath],
  54. define: {
  55. OPENCODE_VERSION: `'${Script.version}'`,
  56. OTUI_TREE_SITTER_WORKER_PATH: "/$bunfs/root/" + path.relative(dir, parserWorker),
  57. OPENCODE_WORKER_PATH: workerPath,
  58. OPENCODE_CHANNEL: `'${Script.channel}'`,
  59. },
  60. })
  61. await $`rm -rf ./dist/${name}/bin/tui`
  62. await Bun.file(`dist/${name}/package.json`).write(
  63. JSON.stringify(
  64. {
  65. name,
  66. version: Script.version,
  67. os: [os === "windows" ? "win32" : os],
  68. cpu: [arch],
  69. },
  70. null,
  71. 2,
  72. ),
  73. )
  74. binaries[name] = Script.version
  75. }
  76. export { binaries }