publish.ts 1.4 KB

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