build.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 skipInstall = process.argv.includes("--skip-install")
  15. const allTargets: {
  16. os: string
  17. arch: "arm64" | "x64"
  18. abi?: "musl"
  19. avx2?: false
  20. }[] = [
  21. {
  22. os: "linux",
  23. arch: "arm64",
  24. },
  25. {
  26. os: "linux",
  27. arch: "x64",
  28. },
  29. {
  30. os: "linux",
  31. arch: "x64",
  32. avx2: false,
  33. },
  34. {
  35. os: "linux",
  36. arch: "arm64",
  37. abi: "musl",
  38. },
  39. {
  40. os: "linux",
  41. arch: "x64",
  42. abi: "musl",
  43. },
  44. {
  45. os: "linux",
  46. arch: "x64",
  47. abi: "musl",
  48. avx2: false,
  49. },
  50. {
  51. os: "darwin",
  52. arch: "arm64",
  53. },
  54. {
  55. os: "darwin",
  56. arch: "x64",
  57. },
  58. {
  59. os: "darwin",
  60. arch: "x64",
  61. avx2: false,
  62. },
  63. {
  64. os: "win32",
  65. arch: "x64",
  66. },
  67. {
  68. os: "win32",
  69. arch: "x64",
  70. avx2: false,
  71. },
  72. ]
  73. const targets = singleFlag
  74. ? allTargets.filter((item) => item.os === process.platform && item.arch === process.arch)
  75. : allTargets
  76. await $`rm -rf dist`
  77. const binaries: Record<string, string> = {}
  78. if (!skipInstall) {
  79. await $`bun install --os="*" --cpu="*" @opentui/core@${pkg.dependencies["@opentui/core"]}`
  80. await $`bun install --os="*" --cpu="*" @parcel/watcher@${pkg.dependencies["@parcel/watcher"]}`
  81. }
  82. for (const item of targets) {
  83. const name = [
  84. pkg.name,
  85. // changing to win32 flags npm for some reason
  86. item.os === "win32" ? "windows" : item.os,
  87. item.arch,
  88. item.avx2 === false ? "baseline" : undefined,
  89. item.abi === undefined ? undefined : item.abi,
  90. ]
  91. .filter(Boolean)
  92. .join("-")
  93. console.log(`building ${name}`)
  94. await $`mkdir -p dist/${name}/bin`
  95. const parserWorker = fs.realpathSync(path.resolve(dir, "./node_modules/@opentui/core/parser.worker.js"))
  96. const workerPath = "./src/cli/cmd/tui/worker.ts"
  97. // Use platform-specific bunfs root path based on target OS
  98. const bunfsRoot = item.os === "win32" ? "B:/~BUN/root/" : "/$bunfs/root/"
  99. const workerRelativePath = path.relative(dir, parserWorker).replaceAll("\\", "/")
  100. await Bun.build({
  101. conditions: ["browser"],
  102. tsconfig: "./tsconfig.json",
  103. plugins: [solidPlugin],
  104. sourcemap: "external",
  105. compile: {
  106. autoloadBunfig: false,
  107. autoloadDotenv: false,
  108. //@ts-ignore (bun types aren't up to date)
  109. autoloadTsconfig: true,
  110. autoloadPackageJson: true,
  111. target: name.replace(pkg.name, "bun") as any,
  112. outfile: `dist/${name}/bin/opencode`,
  113. execArgv: [`--user-agent=opencode/${Script.version}`, "--"],
  114. windows: {},
  115. },
  116. entrypoints: ["./src/index.ts", parserWorker, workerPath],
  117. define: {
  118. OPENCODE_VERSION: `'${Script.version}'`,
  119. OTUI_TREE_SITTER_WORKER_PATH: bunfsRoot + workerRelativePath,
  120. OPENCODE_WORKER_PATH: workerPath,
  121. OPENCODE_CHANNEL: `'${Script.channel}'`,
  122. OPENCODE_LIBC: item.os === "linux" ? `'${item.abi ?? "glibc"}'` : "",
  123. },
  124. })
  125. await $`rm -rf ./dist/${name}/bin/tui`
  126. await Bun.file(`dist/${name}/package.json`).write(
  127. JSON.stringify(
  128. {
  129. name,
  130. version: Script.version,
  131. os: [item.os],
  132. cpu: [item.arch],
  133. },
  134. null,
  135. 2,
  136. ),
  137. )
  138. binaries[name] = Script.version
  139. }
  140. export { binaries }