publish.ts 4.3 KB

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