build.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import fs from "fs"
  4. import { rm } from "fs/promises"
  5. import path from "path"
  6. import { Script } from "@opencode-ai/script"
  7. import { createSolidTransformPlugin } from "@opentui/solid/bun-plugin"
  8. import pkg from "../package.json"
  9. import { modelsData } from "./generate"
  10. const dir = path.resolve(import.meta.dirname, "..")
  11. const binary = "lildax"
  12. process.chdir(dir)
  13. await rm("dist", { recursive: true, force: true })
  14. const singleFlag = process.argv.includes("--single")
  15. const baselineFlag = process.argv.includes("--baseline")
  16. const skipInstall = process.argv.includes("--skip-install")
  17. const sourcemapsFlag = process.argv.includes("--sourcemaps")
  18. const plugin = createSolidTransformPlugin()
  19. const allTargets: {
  20. os: string
  21. arch: "arm64" | "x64"
  22. abi?: "musl"
  23. avx2?: false
  24. }[] = [
  25. { os: "linux", arch: "arm64" },
  26. { os: "linux", arch: "x64" },
  27. { os: "linux", arch: "x64", avx2: false },
  28. { os: "linux", arch: "arm64", abi: "musl" },
  29. { os: "linux", arch: "x64", abi: "musl" },
  30. { os: "linux", arch: "x64", abi: "musl", avx2: false },
  31. { os: "darwin", arch: "arm64" },
  32. { os: "darwin", arch: "x64" },
  33. { os: "darwin", arch: "x64", avx2: false },
  34. { os: "win32", arch: "arm64" },
  35. { os: "win32", arch: "x64" },
  36. { os: "win32", arch: "x64", avx2: false },
  37. ]
  38. const targets = singleFlag
  39. ? allTargets.filter((item) => {
  40. if (item.os !== process.platform || item.arch !== process.arch) return false
  41. if (item.avx2 === false) return baselineFlag
  42. return item.abi === undefined
  43. })
  44. : allTargets
  45. if (!skipInstall) await $`bun install --os="*" --cpu="*" @opentui/core@${pkg.dependencies["@opentui/core"]}`
  46. const localParserWorker = path.resolve(dir, "node_modules/@opentui/core/parser.worker.js")
  47. const rootParserWorker = path.resolve(dir, "../../node_modules/@opentui/core/parser.worker.js")
  48. const parserWorker = fs.realpathSync(fs.existsSync(localParserWorker) ? localParserWorker : rootParserWorker)
  49. for (const item of targets) {
  50. const target = [
  51. binary,
  52. item.os === "win32" ? "windows" : item.os,
  53. item.arch,
  54. item.avx2 === false ? "baseline" : undefined,
  55. item.abi,
  56. ]
  57. .filter(Boolean)
  58. .join("-")
  59. const name = target.replace(binary, "cli")
  60. console.log(`building ${name}`)
  61. const result = await Bun.build({
  62. entrypoints: ["./src/index.ts", parserWorker],
  63. tsconfig: "./tsconfig.json",
  64. plugins: [plugin],
  65. external: ["node-gyp"],
  66. format: "esm",
  67. minify: true,
  68. sourcemap: sourcemapsFlag ? "linked" : "none",
  69. splitting: true,
  70. compile: {
  71. autoloadBunfig: false,
  72. autoloadDotenv: false,
  73. autoloadTsconfig: true,
  74. autoloadPackageJson: true,
  75. target: target.replace(binary, "bun") as Bun.Build.CompileTarget,
  76. outfile: `./dist/${name}/bin/${binary}`,
  77. execArgv: [`--user-agent=${binary}/${Script.version}`, "--use-system-ca", "--"],
  78. windows: {},
  79. },
  80. define: {
  81. OPENCODE_VERSION: `'${Script.version}'`,
  82. OPENCODE_CLI_NAME: `'${binary}'`,
  83. OPENCODE_MODELS_DEV: modelsData,
  84. OPENCODE_CHANNEL: `'${Script.channel}'`,
  85. OPENCODE_LIBC: item.os === "linux" ? `'${item.abi ?? "glibc"}'` : "undefined",
  86. // FFF_LIBC selects the fff native lib variant: "musl" or "gnu".
  87. FFF_LIBC: item.os === "linux" ? `'${item.abi ?? "gnu"}'` : "undefined",
  88. OTUI_TREE_SITTER_WORKER_PATH:
  89. (item.os === "win32" ? '"B:/~BUN/root/' : '"/$bunfs/root/') +
  90. path.relative(dir, parserWorker).replaceAll("\\", "/") +
  91. '"',
  92. ...(item.os === "linux" ? { "process.env.OPENTUI_LIBC": JSON.stringify(item.abi ?? "glibc") } : {}),
  93. },
  94. })
  95. if (!result.success) {
  96. for (const log of result.logs) console.error(log)
  97. process.exit(1)
  98. }
  99. await Bun.write(
  100. `./dist/${name}/package.json`,
  101. JSON.stringify(
  102. {
  103. name: `@opencode-ai/${name}`,
  104. version: Script.version,
  105. license: "MIT",
  106. repository: { type: "git", url: "git+https://github.com/anomalyco/opencode.git" },
  107. os: [item.os],
  108. cpu: [item.arch],
  109. },
  110. null,
  111. 2,
  112. ),
  113. )
  114. }