publish.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. const { binaries } = await import("./build.ts")
  9. {
  10. const name = `${pkg.name}-${process.platform}-${process.arch}`
  11. console.log(`smoke test: running dist/${name}/bin/opencode --version`)
  12. await $`./dist/${name}/bin/opencode --version`
  13. }
  14. await $`mkdir -p ./dist/${pkg.name}`
  15. await $`cp -r ./bin ./dist/${pkg.name}/bin`
  16. await $`cp ./script/postinstall.mjs ./dist/${pkg.name}/postinstall.mjs`
  17. await Bun.file(`./dist/${pkg.name}/package.json`).write(
  18. JSON.stringify(
  19. {
  20. name: pkg.name + "-ai",
  21. bin: {
  22. [pkg.name]: `./bin/${pkg.name}`,
  23. },
  24. scripts: {
  25. postinstall: "bun ./postinstall.mjs || node ./postinstall.mjs",
  26. },
  27. version: Script.version,
  28. optionalDependencies: binaries,
  29. },
  30. null,
  31. 2,
  32. ),
  33. )
  34. const tags = [Script.channel]
  35. const tasks = Object.entries(binaries).map(async ([name]) => {
  36. if (process.platform !== "win32") {
  37. await $`chmod -R 755 .`.cwd(`./dist/${name}`)
  38. }
  39. await $`bun pm pack`.cwd(`./dist/${name}`)
  40. for (const tag of tags) {
  41. await $`npm publish *.tgz --access public --tag ${tag}`.cwd(`./dist/${name}`)
  42. }
  43. })
  44. await Promise.all(tasks)
  45. for (const tag of tags) {
  46. await $`cd ./dist/${pkg.name} && bun pm pack && npm publish *.tgz --access public --tag ${tag}`
  47. }
  48. if (!Script.preview) {
  49. // Create archives for GitHub release
  50. for (const key of Object.keys(binaries)) {
  51. if (key.includes("linux")) {
  52. await $`tar -czf ../../${key}.tar.gz *`.cwd(`dist/${key}/bin`)
  53. } else {
  54. await $`zip -r ../../${key}.zip *`.cwd(`dist/${key}/bin`)
  55. }
  56. }
  57. const image = "ghcr.io/anomalyco/opencode"
  58. const platforms = "linux/amd64,linux/arm64"
  59. const tags = [`${image}:${Script.version}`, `${image}:latest`]
  60. const tagFlags = tags.flatMap((t) => ["-t", t])
  61. await $`docker buildx build --platform ${platforms} ${tagFlags} --push .`
  62. }