publish.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import pkg from "../package.json"
  4. import { Script } from "@opencode-ai/script"
  5. import { fileURLToPath } from "url"
  6. const dir = fileURLToPath(new URL("..", import.meta.url))
  7. process.chdir(dir)
  8. async function published(name: string, version: string) {
  9. return (await $`npm view ${name}@${version} version`.nothrow()).exitCode === 0
  10. }
  11. async function publish(dir: string, name: string, version: string) {
  12. if (process.platform !== "win32") await $`chmod -R 755 .`.cwd(dir)
  13. if (await published(name, version)) return console.log(`already published ${name}@${version}`)
  14. await $`bun pm pack`.cwd(dir)
  15. await $`npm publish *.tgz --access public --tag ${Script.channel}`.cwd(dir)
  16. }
  17. const binaries: Record<string, string> = {}
  18. for (const filepath of new Bun.Glob("*/package.json").scanSync({ cwd: "./dist" })) {
  19. const item = await Bun.file(`./dist/${filepath}`).json()
  20. binaries[item.name] = item.version
  21. }
  22. console.log("binaries", binaries)
  23. const version = Object.values(binaries)[0]
  24. const name = pkg.name
  25. await $`mkdir -p ./dist/${name}/bin`
  26. await $`cp ./bin/opencode2.cjs ./dist/${name}/bin/opencode2`
  27. await Bun.file(`./dist/${name}/package.json`).write(
  28. JSON.stringify(
  29. {
  30. name,
  31. bin: { opencode2: "./bin/opencode2" },
  32. version,
  33. license: pkg.license,
  34. repository: { type: "git", url: "git+https://github.com/anomalyco/opencode.git" },
  35. os: ["darwin", "linux", "win32"],
  36. cpu: ["arm64", "x64"],
  37. optionalDependencies: binaries,
  38. },
  39. null,
  40. 2,
  41. ),
  42. )
  43. await Promise.all(
  44. Object.entries(binaries).map(([name, version]) =>
  45. publish(`./dist/${name.replace("@opencode-ai/", "")}`, name, version),
  46. ),
  47. )
  48. await publish(`./dist/${name}`, name, version)