build-node.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. 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. KILO_MIGRATIONS: JSON.stringify(migrations),
  48. KILO_CHANNEL: `'${Script.channel}'`,
  49. },
  50. files: {
  51. "opencode-web-ui.gen.ts": "",
  52. },
  53. })
  54. console.log("Build complete")