build.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 name = [
  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. console.log(`building ${name}`)
  50. const result = await Bun.build({
  51. entrypoints: ["./src/index.ts"],
  52. tsconfig: "./tsconfig.json",
  53. external: ["node-gyp"],
  54. format: "esm",
  55. minify: true,
  56. sourcemap: sourcemapsFlag ? "linked" : "none",
  57. splitting: true,
  58. compile: {
  59. autoloadBunfig: false,
  60. autoloadDotenv: false,
  61. autoloadTsconfig: true,
  62. autoloadPackageJson: true,
  63. target: name.replace(binary, "bun") as Bun.Build.CompileTarget,
  64. outfile: `./dist/${name}/bin/${binary}`,
  65. execArgv: [`--user-agent=${binary}/${Script.version}`, "--use-system-ca", "--"],
  66. windows: {},
  67. },
  68. define: {
  69. OPENCODE_VERSION: `'${Script.version}'`,
  70. OPENCODE_CLI_NAME: `'${binary}'`,
  71. OPENCODE_MODELS_DEV: modelsData,
  72. OPENCODE_CHANNEL: `'${Script.channel}'`,
  73. OPENCODE_LIBC: item.os === "linux" ? `'${item.abi ?? "glibc"}'` : "undefined",
  74. },
  75. })
  76. if (!result.success) {
  77. for (const log of result.logs) console.error(log)
  78. process.exit(1)
  79. }
  80. await Bun.write(
  81. `./dist/${name}/package.json`,
  82. JSON.stringify(
  83. {
  84. name: `@opencode-ai/${name}`,
  85. version: Script.version,
  86. license: "MIT",
  87. os: [item.os],
  88. cpu: [item.arch],
  89. },
  90. null,
  91. 2,
  92. ),
  93. )
  94. }