2
0

build.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. await Bun.build({
  42. conditions: ["browser"],
  43. tsconfig: "./tsconfig.json",
  44. plugins: [solidPlugin],
  45. sourcemap: "external",
  46. compile: {
  47. target: `bun-${os}-${arch}` as any,
  48. outfile: `dist/${name}/bin/opencode`,
  49. execArgv: [`--user-agent=opencode/${Script.version}`, `--env-file=""`, `--`],
  50. windows: {},
  51. },
  52. entrypoints: ["./src/index.ts", parserWorker, "./src/cli/cmd/tui/worker.ts"],
  53. define: {
  54. OPENCODE_VERSION: `'${Script.version}'`,
  55. OTUI_TREE_SITTER_WORKER_PATH: "/$bunfs/root/" + path.relative(dir, parserWorker),
  56. OPENCODE_CHANNEL: `'${Script.channel}'`,
  57. },
  58. })
  59. await $`rm -rf ./dist/${name}/bin/tui`
  60. await Bun.file(`dist/${name}/package.json`).write(
  61. JSON.stringify(
  62. {
  63. name,
  64. version: Script.version,
  65. os: [os === "windows" ? "win32" : os],
  66. cpu: [arch],
  67. },
  68. null,
  69. 2,
  70. ),
  71. )
  72. binaries[name] = Script.version
  73. }
  74. export { binaries }