publish.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 fetch origin`
  39. await $`git cherry-pick HEAD..origin/dev`.nothrow()
  40. await $`git push origin HEAD --tags --no-verify --force`
  41. const previous = await fetch("https://api.github.com/repos/sst/opencode/releases/latest")
  42. .then((res) => {
  43. if (!res.ok) throw new Error(res.statusText)
  44. return res.json()
  45. })
  46. .then((data) => data.tag_name)
  47. console.log("finding commits between", previous, "and", "HEAD")
  48. const commits = await fetch(`https://api.github.com/repos/sst/opencode/compare/${previous}...HEAD`)
  49. .then((res) => res.json())
  50. .then((data) => data.commits || [])
  51. const raw = commits.map((commit: any) => `- ${commit.commit.message.split("\n").join(" ")}`)
  52. console.log(raw)
  53. const notes =
  54. raw
  55. .filter((x: string) => {
  56. const lower = x.toLowerCase()
  57. return (
  58. !lower.includes("release:") &&
  59. !lower.includes("ignore:") &&
  60. !lower.includes("chore:") &&
  61. !lower.includes("ci:") &&
  62. !lower.includes("wip:") &&
  63. !lower.includes("docs:") &&
  64. !lower.includes("doc:")
  65. )
  66. })
  67. .join("\n") || "No notable changes"
  68. await $`gh release create v${version} --title "v${version}" --notes ${notes} ./packages/opencode/dist/*.zip`
  69. }
  70. if (snapshot) {
  71. await $`git checkout -b snapshot-${version}`
  72. await $`git commit --allow-empty -m "Snapshot release v${version}"`
  73. await $`git tag v${version}`
  74. await $`git push origin v${version} --no-verify`
  75. await $`git checkout dev`
  76. await $`git branch -D snapshot-${version}`
  77. for (const file of pkgjsons) {
  78. await $`git checkout ${tree} ${file}`
  79. }
  80. }