publish.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. const tasks = Object.entries(binaries).map(async ([name]) => {
  37. if (process.platform !== "win32") {
  38. await $`chmod -R 755 .`.cwd(`./dist/${name}`)
  39. }
  40. await $`bun pm pack`.cwd(`./dist/${name}`)
  41. for (const tag of tags) {
  42. await $`npm publish *.tgz --access public --tag ${tag}`.cwd(`./dist/${name}`)
  43. }
  44. })
  45. await Promise.all(tasks)
  46. for (const tag of tags) {
  47. await $`cd ./dist/${pkg.name} && bun pm pack && npm publish *.tgz --access public --tag ${tag}`
  48. }
  49. if (!Script.preview) {
  50. // Create archives for GitHub release
  51. for (const key of Object.keys(binaries)) {
  52. if (key.includes("linux")) {
  53. await $`tar -czf ../../${key}.tar.gz *`.cwd(`dist/${key}/bin`)
  54. } else {
  55. await $`zip -r ../../${key}.zip *`.cwd(`dist/${key}/bin`)
  56. }
  57. }
  58. const image = "ghcr.io/sst/opencode"
  59. const platforms = "linux/amd64,linux/arm64"
  60. const tags = [`${image}:${Script.version}`, `${image}:latest`]
  61. const tagFlags = tags.flatMap((t) => ["-t", t])
  62. await $`docker buildx build --platform ${platforms} ${tagFlags} --push .`
  63. }