publish.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import pkg from "../package.json"
  4. const dry = process.argv.includes("--dry")
  5. const snapshot = process.argv.includes("--snapshot")
  6. const version = snapshot
  7. ? `0.0.0-${new Date().toISOString().slice(0, 16).replace(/[-:T]/g, "")}`
  8. : await $`git describe --tags --exact-match HEAD`
  9. .text()
  10. .then((x) => x.substring(1).trim())
  11. .catch(() => {
  12. console.error("tag not found")
  13. process.exit(1)
  14. })
  15. console.log(`publishing ${version}`)
  16. const GOARCH: Record<string, string> = {
  17. arm64: "arm64",
  18. x64: "amd64",
  19. }
  20. const targets = [
  21. ["linux", "arm64"],
  22. ["linux", "x64"],
  23. ["darwin", "x64"],
  24. ["darwin", "arm64"],
  25. ["windows", "x64"],
  26. ]
  27. await $`rm -rf dist`
  28. const optionalDependencies: Record<string, string> = {}
  29. const npmTag = snapshot ? "snapshot" : "latest"
  30. for (const [os, arch] of targets) {
  31. console.log(`building ${os}-${arch}`)
  32. const name = `${pkg.name}-${os}-${arch}`
  33. await $`mkdir -p dist/${name}/bin`
  34. await $`GOOS=${os} GOARCH=${GOARCH[arch]} go build -ldflags="-s -w -X main.Version=${version}" -o ../opencode/dist/${name}/bin/tui ../tui/cmd/opencode/main.go`.cwd(
  35. "../tui",
  36. )
  37. await $`bun build --define OPENCODE_VERSION="'${version}'" --compile --minify --target=bun-${os}-${arch} --outfile=dist/${name}/bin/opencode ./src/index.ts ./dist/${name}/bin/tui`
  38. await $`rm -rf ./dist/${name}/bin/tui`
  39. await Bun.file(`dist/${name}/package.json`).write(
  40. JSON.stringify(
  41. {
  42. name,
  43. version,
  44. os: [os === "windows" ? "win32" : os],
  45. cpu: [arch],
  46. },
  47. null,
  48. 2,
  49. ),
  50. )
  51. if (!dry)
  52. await $`cd dist/${name} && bun publish --access public --tag ${npmTag}`
  53. optionalDependencies[name] = version
  54. }
  55. await $`mkdir -p ./dist/${pkg.name}`
  56. await $`cp -r ./bin ./dist/${pkg.name}/bin`
  57. await $`cp ./script/postinstall.js ./dist/${pkg.name}/postinstall.js`
  58. await Bun.file(`./dist/${pkg.name}/package.json`).write(
  59. JSON.stringify(
  60. {
  61. name: pkg.name + "-ai",
  62. bin: {
  63. [pkg.name]: `./bin/${pkg.name}`,
  64. },
  65. scripts: {
  66. postinstall: "node ./postinstall.js",
  67. },
  68. version,
  69. optionalDependencies,
  70. },
  71. null,
  72. 2,
  73. ),
  74. )
  75. if (!dry)
  76. await $`cd ./dist/${pkg.name} && bun publish --access public --tag ${npmTag}`
  77. if (!snapshot) {
  78. // Github Release
  79. for (const key of Object.keys(optionalDependencies)) {
  80. await $`cd dist/${key}/bin && zip -r ../../${key}.zip *`
  81. }
  82. const previous = await fetch(
  83. "https://api.github.com/repos/sst/opencode/releases/latest",
  84. )
  85. .then((res) => res.json())
  86. .then((data) => data.tag_name)
  87. const commits = await fetch(
  88. `https://api.github.com/repos/sst/opencode/compare/${previous}...HEAD`,
  89. )
  90. .then((res) => res.json())
  91. .then((data) => data.commits || [])
  92. const notes = commits
  93. .map((commit: any) => `- ${commit.commit.message.split("\n")[0]}`)
  94. .filter((x: string) => {
  95. const lower = x.toLowerCase()
  96. return (
  97. !lower.includes("chore:") &&
  98. !lower.includes("ci:") &&
  99. !lower.includes("docs:")
  100. )
  101. })
  102. .join("\n")
  103. if (!dry)
  104. await $`gh release create v${version} --title "v${version}" --notes ${notes} ./dist/*.zip`
  105. // AUR package
  106. const pkgbuildTemplate = await Bun.file("./script/PKGBUILD.template").text()
  107. const pkgbuild = pkgbuildTemplate
  108. .replace("{{VERSION}}", version.split("-")[0])
  109. .replace(
  110. "{{ARM64_URL}}",
  111. `https://github.com/sst/opencode/releases/download/v${version}/opencode-linux-arm64.zip`,
  112. )
  113. .replace(
  114. "{{ARM64_SHA}}",
  115. await $`sha256sum ./dist/opencode-linux-arm64.zip | cut -d' ' -f1`
  116. .text()
  117. .then((x) => x.trim()),
  118. )
  119. .replace(
  120. "{{X64_URL}}",
  121. `https://github.com/sst/opencode/releases/download/v${version}/opencode-linux-x64.zip`,
  122. )
  123. .replace(
  124. "{{X64_SHA}}",
  125. await $`sha256sum ./dist/opencode-linux-x64.zip | cut -d' ' -f1`
  126. .text()
  127. .then((x) => x.trim()),
  128. )
  129. await $`rm -rf ./dist/aur-opencode-bin`
  130. const gitEnv: Record<string, string> = process.env["AUR_KEY"]
  131. ? { GIT_SSH_COMMAND: `ssh -i ${process.env["AUR_KEY"]}` }
  132. : {}
  133. await $`git clone ssh://[email protected]/opencode-bin.git ./dist/aur-opencode-bin`.env(
  134. gitEnv,
  135. )
  136. await Bun.file("./dist/aur-opencode-bin/PKGBUILD").write(pkgbuild)
  137. await $`cd ./dist/aur-opencode-bin && makepkg --printsrcinfo > .SRCINFO`
  138. await $`cd ./dist/aur-opencode-bin && git add PKGBUILD .SRCINFO`.env(gitEnv)
  139. await $`cd ./dist/aur-opencode-bin && git commit -m "Update to v${version}"`.env(
  140. gitEnv,
  141. )
  142. if (!dry) await $`cd ./dist/aur-opencode-bin && git push`.env(gitEnv)
  143. }