publish.ts 3.4 KB

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