publish.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import { createOpencodeClient, createOpencodeServer } from "@opencode-ai/sdk"
  4. if (process.versions.bun !== "1.2.21") {
  5. throw new Error("This script requires [email protected]")
  6. }
  7. let notes = []
  8. console.log("=== publishing ===\n")
  9. const snapshot = process.env["OPENCODE_SNAPSHOT"] === "true"
  10. const version = await (async () => {
  11. if (snapshot) return `0.0.0-${new Date().toISOString().slice(0, 16).replace(/[-:T]/g, "")}`
  12. if (process.env["OPENCODE_VERSION"]) return process.env["OPENCODE_VERSION"]
  13. const [major, minor, patch] = (await $`gh release list --limit 1 --json tagName --jq '.[0].tagName'`.text())
  14. .trim()
  15. .replace(/^v/, "")
  16. .split(".")
  17. .map((x) => Number(x) || 0)
  18. const t = process.env["OPENCODE_BUMP"]?.toLowerCase()
  19. if (t === "major") return `${major + 1}.0.0`
  20. if (t === "minor") return `${major}.${minor + 1}.0`
  21. return `${major}.${minor}.${patch + 1}`
  22. })()
  23. process.env["OPENCODE_VERSION"] = version
  24. console.log("version:", version)
  25. if (!snapshot) {
  26. const previous = await fetch("https://api.github.com/repos/sst/opencode/releases/latest")
  27. .then((res) => {
  28. if (!res.ok) throw new Error(res.statusText)
  29. return res.json()
  30. })
  31. .then((data) => data.tag_name)
  32. const server = await createOpencodeServer()
  33. const client = createOpencodeClient({ baseUrl: server.url })
  34. const session = await client.session.create()
  35. console.log("generating changelog since " + previous)
  36. const raw = await client.session
  37. .prompt({
  38. path: {
  39. id: session.data!.id,
  40. },
  41. body: {
  42. model: {
  43. providerID: "opencode",
  44. modelID: "kimi-k2",
  45. },
  46. parts: [
  47. {
  48. type: "text",
  49. text: `
  50. Analyze the commits between ${previous} and HEAD.
  51. We care about changes to
  52. - packages/opencode
  53. - packages/sdk
  54. - packages/plugin
  55. We do not care about anything else
  56. Return a changelog of all notable user facing changes.
  57. - Do NOT make general statements about "improvements", be very specific about what was changed.
  58. - Do NOT include any information about code changes if they do not affect the user facing changes.
  59. IMPORTANT: ONLY return a bulleted list of changes, do not include any other information. Do not include a preamble like "Based on my analysis..."
  60. <example>
  61. - Added ability to @ mention agents
  62. - Fixed a bug where the TUI would render improperly on some terminals
  63. </example>
  64. `,
  65. },
  66. ],
  67. },
  68. })
  69. .then((x) => x.data?.parts?.find((y) => y.type === "text")?.text)
  70. for (const line of raw?.split("\n") ?? []) {
  71. if (line.startsWith("- ")) {
  72. notes.push(line)
  73. }
  74. }
  75. console.log(notes)
  76. server.close()
  77. }
  78. const pkgjsons = await Array.fromAsync(
  79. new Bun.Glob("**/package.json").scan({
  80. absolute: true,
  81. }),
  82. ).then((arr) => arr.filter((x) => !x.includes("node_modules") && !x.includes("dist")))
  83. const tree = await $`git add . && git write-tree`.text().then((x) => x.trim())
  84. for (const file of pkgjsons) {
  85. let pkg = await Bun.file(file).text()
  86. pkg = pkg.replaceAll(/"version": "[^"]+"/g, `"version": "${version}"`)
  87. console.log("updated:", file)
  88. await Bun.file(file).write(pkg)
  89. }
  90. await $`bun install`
  91. console.log("\n=== opencode ===\n")
  92. await import(`../packages/opencode/script/publish.ts`)
  93. console.log("\n=== sdk ===\n")
  94. await import(`../packages/sdk/js/script/publish.ts`)
  95. console.log("\n=== plugin ===\n")
  96. await import(`../packages/plugin/script/publish.ts`)
  97. const dir = new URL("..", import.meta.url).pathname
  98. process.chdir(dir)
  99. if (!snapshot) {
  100. await $`git commit -am "release: v${version}"`
  101. await $`git tag v${version}`
  102. await $`git fetch origin`
  103. await $`git cherry-pick HEAD..origin/dev`.nothrow()
  104. await $`git push origin HEAD --tags --no-verify --force`
  105. await $`gh release create v${version} --title "v${version}" --notes ${notes.join("\n") ?? "No notable changes"} ./packages/opencode/dist/*.zip`
  106. }
  107. if (snapshot) {
  108. await $`git checkout -b snapshot-${version}`
  109. await $`git commit --allow-empty -m "Snapshot release v${version}"`
  110. await $`git tag v${version}`
  111. await $`git push origin v${version} --no-verify`
  112. await $`git checkout dev`
  113. await $`git branch -D snapshot-${version}`
  114. for (const file of pkgjsons) {
  115. await $`git checkout ${tree} ${file}`
  116. }
  117. }