publish.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. imports: Record<string, Record<string, string>>
  13. }
  14. const tarball = `${pkg.name.replace("@", "").replace("/", "-")}-${pkg.version}.tgz`
  15. if ((await $`npm view ${pkg.name}@${pkg.version} version`.nothrow()).exitCode === 0) {
  16. console.log(`already published ${pkg.name}@${pkg.version}`)
  17. process.exit(0)
  18. }
  19. const output = (value: string, extension: ".js" | ".d.ts") =>
  20. value.replace("./src/", "./dist/").replace(/\.ts$/, extension)
  21. try {
  22. await $`bun run typecheck`
  23. await $`bun run build`
  24. pkg.exports = Object.fromEntries(
  25. Object.entries(pkg.exports).map(([key, value]) => {
  26. if (typeof value !== "string") return [key, value]
  27. return [key, { import: output(value, ".js"), types: output(value, ".d.ts") }]
  28. }),
  29. )
  30. pkg.imports = Object.fromEntries(
  31. Object.entries(pkg.imports).map(([key, conditions]) => [
  32. key,
  33. Object.fromEntries(Object.entries(conditions).map(([condition, value]) => [condition, output(value, ".js")])),
  34. ]),
  35. )
  36. await Bun.write("package.json", JSON.stringify(pkg, null, 2) + "\n")
  37. await rm(tarball, { force: true })
  38. await $`bun pm pack`
  39. await $`npm publish ${tarball} --tag ${Script.channel} --access public`
  40. } finally {
  41. await Bun.write("package.json", originalText)
  42. await rm(tarball, { force: true })
  43. }