build.ts 2.9 KB

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