publish.ts 3.0 KB

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