build-node.ts 1.5 KB

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