build-node.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env bun
  2. import { Script } from "@opencode-ai/script"
  3. import fs from "fs"
  4. import path from "path"
  5. import { fileURLToPath } from "url"
  6. const __filename = fileURLToPath(import.meta.url)
  7. const __dirname = path.dirname(__filename)
  8. const dir = path.resolve(__dirname, "..")
  9. process.chdir(dir)
  10. const generated = await import("./generate.ts")
  11. // Load migrations from migration directories
  12. const migrationDirs = (
  13. await fs.promises.readdir(path.join(dir, "migration"), {
  14. withFileTypes: true,
  15. })
  16. )
  17. .filter((entry) => entry.isDirectory() && /^\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}/.test(entry.name))
  18. .map((entry) => entry.name)
  19. .sort()
  20. const migrations = await Promise.all(
  21. migrationDirs.map(async (name) => {
  22. const file = path.join(dir, "migration", name, "migration.sql")
  23. const sql = await Bun.file(file).text()
  24. const match = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/.exec(name)
  25. const timestamp = match
  26. ? Date.UTC(
  27. Number(match[1]),
  28. Number(match[2]) - 1,
  29. Number(match[3]),
  30. Number(match[4]),
  31. Number(match[5]),
  32. Number(match[6]),
  33. )
  34. : 0
  35. return { sql, timestamp, name }
  36. }),
  37. )
  38. console.log(`Loaded ${migrations.length} migrations`)
  39. await Bun.build({
  40. target: "node",
  41. entrypoints: ["./src/node.ts"],
  42. outdir: "./dist/node",
  43. format: "esm",
  44. sourcemap: "linked",
  45. external: ["jsonc-parser", "@lydell/node-pty"],
  46. define: {
  47. OPENCODE_MIGRATIONS: JSON.stringify(migrations),
  48. OPENCODE_MODELS_DEV: generated.modelsData,
  49. OPENCODE_CHANNEL: `'${Script.channel}'`,
  50. },
  51. files: {
  52. "opencode-web-ui.gen.ts": "",
  53. },
  54. })
  55. console.log("Build complete")