build.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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(
  35. path.join(dir, "../../node_modules"),
  36. )
  37. await $`tar -xf ../../node_modules/${opentui.replace("@opentui/", "opentui-")}-*.tgz -C ../../node_modules/${opentui} --strip-components=1`
  38. const watcher = `@parcel/watcher-${os === "windows" ? "win32" : os}-${arch.replace("-baseline", "")}${os === "linux" ? "-glibc" : ""}`
  39. await $`mkdir -p ../../node_modules/${watcher}`
  40. await $`npm pack ${watcher}`.cwd(path.join(dir, "../../node_modules")).quiet()
  41. await $`tar -xf ../../node_modules/${watcher.replace("@parcel/", "parcel-")}-*.tgz -C ../../node_modules/${watcher} --strip-components=1`
  42. const parserWorker = fs.realpathSync(
  43. path.resolve(dir, "./node_modules/@opentui/core/parser.worker.js"),
  44. )
  45. const workerPath = "./src/cli/cmd/tui/worker.ts"
  46. await Bun.build({
  47. conditions: ["browser"],
  48. tsconfig: "./tsconfig.json",
  49. plugins: [solidPlugin],
  50. sourcemap: "external",
  51. compile: {
  52. target: `bun-${os}-${arch}` as any,
  53. outfile: `dist/${name}/bin/opencode`,
  54. execArgv: [`--user-agent=opencode/${Script.version}`, `--env-file=""`, `--`],
  55. windows: {},
  56. },
  57. entrypoints: ["./src/index.ts", parserWorker, workerPath],
  58. define: {
  59. OPENCODE_VERSION: `'${Script.version}'`,
  60. OTUI_TREE_SITTER_WORKER_PATH: "/$bunfs/root/" + path.relative(dir, parserWorker),
  61. OPENCODE_WORKER_PATH: workerPath,
  62. OPENCODE_CHANNEL: `'${Script.channel}'`,
  63. },
  64. })
  65. await $`rm -rf ./dist/${name}/bin/tui`
  66. await Bun.file(`dist/${name}/package.json`).write(
  67. JSON.stringify(
  68. {
  69. name,
  70. version: Script.version,
  71. os: [os === "windows" ? "win32" : os],
  72. cpu: [arch],
  73. },
  74. null,
  75. 2,
  76. ),
  77. )
  78. binaries[name] = Script.version
  79. }
  80. export { binaries }