publish.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. async function publishDistribution(input: { root: string; name: string; binary: string; packagePrefix: string }) {
  18. const binaries: Record<string, string> = {}
  19. for (const filepath of new Bun.Glob("*/package.json").scanSync({ cwd: input.root })) {
  20. const item = await Bun.file(`${input.root}/${filepath}`).json()
  21. if (!item.name.startsWith(input.packagePrefix)) continue
  22. binaries[item.name] = item.version
  23. }
  24. console.log(input.name, "binaries", binaries)
  25. const versions = new Set(Object.values(binaries))
  26. if (versions.size > 1) throw new Error(`Binary package versions do not match for ${input.name}`)
  27. const version = versions.values().next().value
  28. if (!version) throw new Error(`No binary packages found for ${input.name}`)
  29. await $`mkdir -p ${input.root}/${input.name}/bin`
  30. await $`cp ./script/postinstall.mjs ${input.root}/${input.name}/postinstall.mjs`
  31. await Bun.file(`${input.root}/${input.name}/bin/${input.binary}.exe`).write(
  32. [
  33. `echo "Error: ${input.name}'s postinstall script was not run." >&2`,
  34. 'echo "" >&2',
  35. 'echo "This occurs when installation scripts are disabled." >&2',
  36. 'echo "Run the package postinstall script or reinstall with scripts enabled." >&2',
  37. "exit 1",
  38. "",
  39. ].join("\n"),
  40. )
  41. await Bun.file(`${input.root}/${input.name}/package.json`).write(
  42. JSON.stringify(
  43. {
  44. name: input.name,
  45. bin: { [input.binary]: `./bin/${input.binary}.exe` },
  46. scripts: { postinstall: "node ./postinstall.mjs" },
  47. version,
  48. license: pkg.license,
  49. repository: { type: "git", url: "git+https://github.com/anomalyco/opencode.git" },
  50. os: ["darwin", "linux", "win32"],
  51. cpu: ["arm64", "x64"],
  52. optionalDependencies: binaries,
  53. },
  54. null,
  55. 2,
  56. ),
  57. )
  58. await Promise.all(
  59. Object.entries(binaries).map(([name, version]) =>
  60. publish(`${input.root}/${name.replace("@opencode-ai/", "")}`, name, version),
  61. ),
  62. )
  63. await publish(`${input.root}/${input.name}`, input.name, version)
  64. }
  65. await publishDistribution({
  66. root: "./dist",
  67. name: pkg.name,
  68. binary: "opencode2",
  69. packagePrefix: "@opencode-ai/cli-",
  70. })
  71. await publishDistribution({
  72. root: "./dist/node",
  73. name: "opencode-node",
  74. binary: "opencode2-node",
  75. packagePrefix: "@opencode-ai/cli-node-",
  76. })