build-node.ts 1.4 KB

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