publish.ts 2.5 KB

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