pack.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import { rm } from "node:fs/promises"
  4. import path from "node:path"
  5. export async function pack() {
  6. const original = await Bun.file("package.json").text()
  7. const pkg = JSON.parse(original) as {
  8. name: string
  9. version: string
  10. exports: Record<string, string | { types: string; import: string }>
  11. }
  12. const tarball = path.resolve(`${pkg.name.replace("@", "").replace("/", "-")}-${pkg.version}.tgz`)
  13. await $`bun run build`
  14. pkg.exports = Object.fromEntries(
  15. Object.entries(pkg.exports).map(([key, value]) => {
  16. if (typeof value !== "string" || (!value.endsWith(".ts") && !value.endsWith(".tsx"))) return [key, value]
  17. return [
  18. key,
  19. {
  20. types: value.replace("./src/", "./dist/").replace(/\.tsx?$/, ".d.ts"),
  21. import: value,
  22. },
  23. ]
  24. }),
  25. )
  26. await rm(tarball, { force: true })
  27. await Bun.write("package.json", JSON.stringify(pkg, null, 2) + "\n")
  28. try {
  29. await $`bun pm pack`
  30. return tarball
  31. } finally {
  32. await Bun.write("package.json", original)
  33. }
  34. }
  35. if (import.meta.main) console.log(await pack())