pack.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import { fileURLToPath } from "node:url"
  4. const dir = fileURLToPath(new URL("..", import.meta.url))
  5. export const pack = async () => {
  6. process.chdir(dir)
  7. await $`bun run build`
  8. const original = await Bun.file("package.json").text()
  9. // oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- package.json is validated by the package schema and build checks.
  10. const pkg = JSON.parse(original) as {
  11. readonly version: string
  12. exports: Record<string, string | { readonly import: string; readonly types: string }>
  13. }
  14. for (const [key, value] of Object.entries(pkg.exports)) {
  15. if (typeof value !== "string") continue
  16. const file = value.replace("./src/", "./dist/").replace(/\.ts$/, "")
  17. pkg.exports[key] = { import: `${file}.js`, types: `${file}.d.ts` }
  18. }
  19. await Bun.write("package.json", JSON.stringify(pkg, null, 2))
  20. try {
  21. await $`bun pm pack`
  22. return fileURLToPath(new URL(`../opencode-ai-http-recorder-${pkg.version}.tgz`, import.meta.url))
  23. } finally {
  24. await Bun.write("package.json", original)
  25. }
  26. }
  27. export const withPackedArchive = async <A>(use: (archive: string) => Promise<A>) => {
  28. const archive = await pack()
  29. try {
  30. return await use(archive)
  31. } finally {
  32. const file = Bun.file(archive)
  33. if (await file.exists()) await file.delete()
  34. }
  35. }
  36. if (import.meta.main) await pack()