publish.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env bun
  2. import { Script } from "@opencode-ai/script"
  3. import { $ } from "bun"
  4. import { Effect } from "effect"
  5. import { fileURLToPath } from "url"
  6. const dir = fileURLToPath(new URL("..", import.meta.url))
  7. process.chdir(dir)
  8. const packageJson = (value: unknown) => {
  9. if (
  10. typeof value === "object" &&
  11. value !== null &&
  12. "name" in value &&
  13. typeof value.name === "string" &&
  14. "version" in value &&
  15. typeof value.version === "string" &&
  16. "exports" in value &&
  17. typeof value.exports === "object" &&
  18. value.exports !== null
  19. ) {
  20. return {
  21. name: value.name,
  22. version: value.version,
  23. exports: value.exports,
  24. }
  25. }
  26. throw new Error("invalid sdk package manifest")
  27. }
  28. const published = (name: string, version: string) =>
  29. Effect.promise(() => $`npm view ${name}@${version} version`.nothrow()).pipe(
  30. Effect.map((result) => result.exitCode === 0),
  31. )
  32. const withPackageJson = (pkg: ReturnType<typeof packageJson>, next: ReturnType<typeof packageJson>) =>
  33. Effect.promise(() => Bun.write("package.json", JSON.stringify(next, null, 2))).pipe(
  34. Effect.zipRight(Effect.promise(() => $`bun pm pack`)),
  35. Effect.zipRight(Effect.promise(() => $`npm publish *.tgz --tag ${Script.channel} --access public`)),
  36. Effect.ensuring(Effect.promise(() => Bun.write("package.json", JSON.stringify(pkg, null, 2)))),
  37. )
  38. function transformExports(exports: Record<string, unknown>) {
  39. return Object.fromEntries(
  40. Object.entries(exports).map(([key, value]) => {
  41. if (typeof value === "string") {
  42. const file = value.replace("./src/", "./dist/").replace(".ts", "")
  43. return [key, { import: file + ".js", types: file + ".d.ts" }]
  44. }
  45. if (typeof value === "object" && value !== null && !Array.isArray(value)) return [key, transformExports(value)]
  46. return [key, value]
  47. }),
  48. )
  49. }
  50. const program = Effect.gen(function* () {
  51. const pkg = packageJson(yield* Effect.promise(() => import("../package.json").then((m) => m.default)))
  52. if (yield* published(pkg.name, pkg.version)) {
  53. console.log(`already published ${pkg.name}@${pkg.version}`)
  54. return
  55. }
  56. const next = {
  57. ...pkg,
  58. exports: transformExports(pkg.exports),
  59. }
  60. yield* withPackageJson(pkg, next)
  61. })
  62. await Effect.runPromise(program)