publish.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env bun
  2. import { Script } from "@opencode-ai/script"
  3. import { $ } from "bun"
  4. import { rm } from "node:fs/promises"
  5. import { fileURLToPath } from "node:url"
  6. process.chdir(fileURLToPath(new URL("..", import.meta.url)))
  7. const originalText = await Bun.file("package.json").text()
  8. const pkg = JSON.parse(originalText) as {
  9. name: string
  10. version: string
  11. exports: Record<string, string | { import: string; types: string }>
  12. }
  13. const tarball = `${pkg.name.replace("@", "").replace("/", "-")}-${pkg.version}.tgz`
  14. if ((await $`npm view ${pkg.name}@${pkg.version} version`.nothrow()).exitCode === 0) {
  15. console.log(`already published ${pkg.name}@${pkg.version}`)
  16. process.exit(0)
  17. }
  18. try {
  19. await $`bun run typecheck`
  20. await $`bun run build`
  21. pkg.exports = Object.fromEntries(
  22. Object.entries(pkg.exports).map(([key, value]) => {
  23. if (typeof value !== "string") return [key, value]
  24. return [
  25. key,
  26. {
  27. import: value.replace("./src/", "./dist/").replace(/\.ts$/, ".js"),
  28. types: value.replace("./src/", "./dist/").replace(/\.ts$/, ".d.ts"),
  29. },
  30. ]
  31. }),
  32. )
  33. await Bun.write("package.json", JSON.stringify(pkg, null, 2) + "\n")
  34. await rm(tarball, { force: true })
  35. await $`bun pm pack`
  36. await $`npm publish ${tarball} --tag ${Script.channel} --access public`
  37. } finally {
  38. await Bun.write("package.json", originalText)
  39. await rm(tarball, { force: true })
  40. }