publish.ts 962 B

1234567891011121314151617181920212223242526
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import { fileURLToPath } from "node:url"
  4. import { pack } from "./pack.js"
  5. import { verifyPackage } from "./verify-package.js"
  6. const dir = fileURLToPath(new URL("..", import.meta.url))
  7. process.chdir(dir)
  8. const published = async (name: string, version: string) =>
  9. (await $`npm view ${name}@${version} version`.nothrow()).exitCode === 0
  10. // oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- package.json is validated by the package schema and build checks.
  11. const pkg = JSON.parse(await Bun.file("package.json").text()) as { readonly name: string; readonly version: string }
  12. if (await published(pkg.name, pkg.version)) {
  13. console.log(`already published ${pkg.name}@${pkg.version}`)
  14. } else {
  15. const archive = await pack()
  16. try {
  17. await verifyPackage(archive)
  18. await $`npm publish ${archive} --tag beta --access public --provenance`
  19. } finally {
  20. await Bun.file(archive).delete()
  21. }
  22. }