build.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env bun
  2. import path from "path"
  3. import fs from "fs"
  4. import { $ } from "bun"
  5. import { fileURLToPath } from "url"
  6. const __filename = fileURLToPath(import.meta.url)
  7. const __dirname = path.dirname(__filename)
  8. const dir = path.resolve(__dirname, "..")
  9. // Resolve @opentui/solid package path more robustly using import.meta.resolve
  10. const solidPackagePath = path.dirname(fileURLToPath(import.meta.resolve("@opentui/solid")))
  11. const solidPluginPath = path.join(solidPackagePath, "scripts/solid-plugin.ts")
  12. const solidPlugin = (await import(solidPluginPath)).default
  13. process.chdir(dir)
  14. import pkg from "../package.json"
  15. import { Script } from "@opencode-ai/script"
  16. const singleFlag = process.argv.includes("--single")
  17. const allTargets: {
  18. os: string
  19. arch: "arm64" | "x64"
  20. abi?: "musl"
  21. avx2?: false
  22. }[] = [
  23. {
  24. os: "linux",
  25. arch: "arm64",
  26. },
  27. {
  28. os: "linux",
  29. arch: "x64",
  30. },
  31. {
  32. os: "linux",
  33. arch: "x64",
  34. avx2: false,
  35. },
  36. {
  37. os: "linux",
  38. arch: "arm64",
  39. abi: "musl",
  40. },
  41. {
  42. os: "linux",
  43. arch: "x64",
  44. abi: "musl",
  45. },
  46. {
  47. os: "linux",
  48. arch: "x64",
  49. abi: "musl",
  50. avx2: false,
  51. },
  52. {
  53. os: "darwin",
  54. arch: "arm64",
  55. },
  56. {
  57. os: "darwin",
  58. arch: "x64",
  59. },
  60. {
  61. os: "darwin",
  62. arch: "x64",
  63. avx2: false,
  64. },
  65. {
  66. os: "win32",
  67. arch: "x64",
  68. },
  69. {
  70. os: "win32",
  71. arch: "x64",
  72. avx2: false,
  73. },
  74. ]
  75. const targets = singleFlag
  76. ? allTargets.filter((item) => item.os === process.platform && item.arch === process.arch)
  77. : allTargets
  78. await $`rm -rf dist`
  79. const binaries: Record<string, string> = {}
  80. await $`bun install --os="*" --cpu="*" @opentui/core@${pkg.dependencies["@opentui/core"]}`
  81. await $`bun install --os="*" --cpu="*" @parcel/watcher@${pkg.dependencies["@parcel/watcher"]}`
  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. await Bun.build({
  98. conditions: ["browser"],
  99. tsconfig: "./tsconfig.json",
  100. plugins: [solidPlugin],
  101. sourcemap: "external",
  102. compile: {
  103. target: name.replace(pkg.name, "bun") as any,
  104. outfile: `dist/${name}/bin/opencode`,
  105. execArgv: [`--user-agent=opencode/${Script.version}`, `--env-file=""`, `--`],
  106. windows: {},
  107. },
  108. entrypoints: ["./src/index.ts", parserWorker, workerPath],
  109. define: {
  110. OPENCODE_VERSION: `'${Script.version}'`,
  111. OTUI_TREE_SITTER_WORKER_PATH: "/$bunfs/root/" + path.relative(dir, parserWorker).replaceAll("\\", "/"),
  112. OPENCODE_WORKER_PATH: workerPath,
  113. OPENCODE_CHANNEL: `'${Script.channel}'`,
  114. },
  115. })
  116. await $`rm -rf ./dist/${name}/bin/tui`
  117. await Bun.file(`dist/${name}/package.json`).write(
  118. JSON.stringify(
  119. {
  120. name,
  121. version: Script.version,
  122. os: [item.os],
  123. cpu: [item.arch],
  124. },
  125. null,
  126. 2,
  127. ),
  128. )
  129. binaries[name] = Script.version
  130. }
  131. export { binaries }