build.ts 3.5 KB

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