publish.ts 2.9 KB

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