publish.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import path from "path"
  4. console.log("=== publishing ===\n")
  5. const snapshot = process.env["OPENCODE_SNAPSHOT"] === "true"
  6. const version = snapshot
  7. ? `0.0.0-${new Date().toISOString().slice(0, 16).replace(/[-:T]/g, "")}`
  8. : process.env["OPENCODE_VERSION"]
  9. if (!version) {
  10. throw new Error("OPENCODE_VERSION is required")
  11. }
  12. process.env["OPENCODE_VERSION"] = version
  13. console.log("version:", version)
  14. const pkgjsons = await Array.fromAsync(
  15. new Bun.Glob("**/package.json").scan({
  16. absolute: true,
  17. }),
  18. ).then((arr) => arr.filter((x) => !x.includes("node_modules") && !x.includes("dist")))
  19. const tree = await $`git add . && git write-tree`.text().then((x) => x.trim())
  20. for (const file of pkgjsons) {
  21. let pkg = await Bun.file(file).text()
  22. pkg = pkg.replaceAll(/"version": "[^"]+"/g, `"version": "${version}"`)
  23. console.log("updated:", file)
  24. await Bun.file(file).write(pkg)
  25. }
  26. await $`bun install`
  27. console.log("\n=== opencode ===\n")
  28. await import(`../packages/opencode/script/publish.ts`)
  29. console.log("\n=== sdk ===\n")
  30. await import(`../packages/sdk/js/script/publish.ts`)
  31. console.log("\n=== plugin ===\n")
  32. await import(`../packages/plugin/script/publish.ts`)
  33. const dir = new URL("..", import.meta.url).pathname
  34. process.chdir(dir)
  35. if (!snapshot) {
  36. await $`git commit -am "release: v${version}"`
  37. await $`git tag v${version}`
  38. await $`git push origin HEAD --tags --no-verify`
  39. const previous = await fetch("https://api.github.com/repos/sst/opencode/releases/latest")
  40. .then((res) => {
  41. if (!res.ok) throw new Error(res.statusText)
  42. return res.json()
  43. })
  44. .then((data) => data.tag_name)
  45. console.log("finding commits between", previous, "and", "HEAD")
  46. const commits = await fetch(`https://api.github.com/repos/sst/opencode/compare/${previous}...HEAD`)
  47. .then((res) => res.json())
  48. .then((data) => data.commits || [])
  49. const raw = commits.map((commit: any) => `- ${commit.commit.message.split("\n").join(" ")}`)
  50. console.log(raw)
  51. const notes =
  52. raw
  53. .filter((x: string) => {
  54. const lower = x.toLowerCase()
  55. return (
  56. !lower.includes("release:") &&
  57. !lower.includes("ignore:") &&
  58. !lower.includes("chore:") &&
  59. !lower.includes("ci:") &&
  60. !lower.includes("wip:") &&
  61. !lower.includes("docs:") &&
  62. !lower.includes("doc:")
  63. )
  64. })
  65. .join("\n") || "No notable changes"
  66. await $`gh release create v${version} --title "v${version}" --notes ${notes} ./packages/opencode/dist/*.zip`
  67. }
  68. if (snapshot) {
  69. await $`git checkout -b snapshot-${version}`
  70. await $`git commit --allow-empty -m "Snapshot release v${version}"`
  71. await $`git tag v${version}`
  72. await $`git push origin v${version} --no-verify`
  73. await $`git checkout dev`
  74. await $`git branch -D snapshot-${version}`
  75. for (const file of pkgjsons) {
  76. await $`git checkout ${tree} ${file}`
  77. }
  78. }