publish-start.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import { createOpencode } from "@opencode-ai/sdk"
  4. import { Script } from "@opencode-ai/script"
  5. const notes = [] as string[]
  6. console.log("=== publishing ===\n")
  7. if (!Script.preview) {
  8. const previous = await fetch("https://registry.npmjs.org/opencode-ai/latest")
  9. .then((res) => {
  10. if (!res.ok) throw new Error(res.statusText)
  11. return res.json()
  12. })
  13. .then((data: any) => data.version)
  14. const log =
  15. await $`git log v${previous}..HEAD --oneline --format="%h %s" -- packages/opencode packages/sdk packages/plugin packages/tauri packages/desktop`.text()
  16. const commits = log
  17. .split("\n")
  18. .filter((line) => line && !line.match(/^\w+ (ignore:|test:|chore:|ci:)/i))
  19. .join("\n")
  20. const opencode = await createOpencode()
  21. const session = await opencode.client.session.create()
  22. console.log("generating changelog since " + previous)
  23. const raw = await opencode.client.session
  24. .prompt({
  25. path: {
  26. id: session.data!.id,
  27. },
  28. body: {
  29. model: {
  30. providerID: "opencode",
  31. modelID: "claude-haiku-4-5",
  32. },
  33. parts: [
  34. {
  35. type: "text",
  36. text: `
  37. Analyze these commits and generate a changelog of all notable user facing changes.
  38. Commits between ${previous} and HEAD:
  39. ${commits}
  40. - Do NOT make general statements about "improvements", be very specific about what was changed.
  41. - Do NOT include any information about code changes if they do not affect the user facing changes.
  42. - For commits that are already well-written and descriptive, avoid rewording them. Simply capitalize the first letter, fix any misspellings, and ensure proper English grammar.
  43. - DO NOT read any other commits than the ones listed above (THIS IS IMPORTANT TO AVOID DUPLICATING THINGS IN OUR CHANGELOG)
  44. - If a commit was made and then reverted do not include it in the changelog. If the commits only include a revert but not the original commit, then include the revert in the changelog.
  45. IMPORTANT: ONLY return a bulleted list of changes, do not include any other information. Do not include a preamble like "Based on my analysis..."
  46. <example>
  47. - Added ability to @ mention agents
  48. - Fixed a bug where the TUI would render improperly on some terminals
  49. </example>
  50. `,
  51. },
  52. ],
  53. },
  54. })
  55. .then((x) => x.data?.parts?.find((y) => y.type === "text")?.text)
  56. for (const line of raw?.split("\n") ?? []) {
  57. if (line.startsWith("- ")) {
  58. notes.push(line)
  59. }
  60. }
  61. console.log("---- Generated Changelog ----")
  62. console.log(notes.join("\n"))
  63. console.log("-----------------------------")
  64. opencode.server.close()
  65. // Get contributors
  66. const team = [
  67. "actions-user",
  68. "opencode",
  69. "rekram1-node",
  70. "thdxr",
  71. "kommander",
  72. "jayair",
  73. "fwang",
  74. "adamdotdevin",
  75. "iamdavidhill",
  76. "opencode-agent[bot]",
  77. ]
  78. const compare =
  79. await $`gh api "/repos/sst/opencode/compare/v${previous}...HEAD" --jq '.commits[] | {login: .author.login, message: .commit.message}'`.text()
  80. const contributors = new Map<string, string[]>()
  81. for (const line of compare.split("\n").filter(Boolean)) {
  82. const { login, message } = JSON.parse(line) as { login: string | null; message: string }
  83. const title = message.split("\n")[0] ?? ""
  84. if (title.match(/^(ignore:|test:|chore:|ci:|release:)/i)) continue
  85. if (login && !team.includes(login)) {
  86. if (!contributors.has(login)) contributors.set(login, [])
  87. contributors.get(login)?.push(title)
  88. }
  89. }
  90. if (contributors.size > 0) {
  91. notes.push("")
  92. notes.push(`**Thank you to ${contributors.size} community contributor${contributors.size > 1 ? "s" : ""}:**`)
  93. for (const [username, userCommits] of contributors) {
  94. notes.push(`- @${username}:`)
  95. for (const commit of userCommits) {
  96. notes.push(` - ${commit}`)
  97. }
  98. }
  99. }
  100. }
  101. const pkgjsons = await Array.fromAsync(
  102. new Bun.Glob("**/package.json").scan({
  103. absolute: true,
  104. }),
  105. ).then((arr) => arr.filter((x) => !x.includes("node_modules") && !x.includes("dist")))
  106. for (const file of pkgjsons) {
  107. let pkg = await Bun.file(file).text()
  108. pkg = pkg.replaceAll(/"version": "[^"]+"/g, `"version": "${Script.version}"`)
  109. console.log("updated:", file)
  110. await Bun.file(file).write(pkg)
  111. }
  112. const extensionToml = new URL("../packages/extensions/zed/extension.toml", import.meta.url).pathname
  113. let toml = await Bun.file(extensionToml).text()
  114. toml = toml.replace(/^version = "[^"]+"/m, `version = "${Script.version}"`)
  115. toml = toml.replaceAll(/releases\/download\/v[^/]+\//g, `releases/download/v${Script.version}/`)
  116. console.log("updated:", extensionToml)
  117. await Bun.file(extensionToml).write(toml)
  118. await $`bun install`
  119. console.log("\n=== opencode ===\n")
  120. await import(`../packages/opencode/script/publish.ts`)
  121. console.log("\n=== sdk ===\n")
  122. await import(`../packages/sdk/js/script/publish.ts`)
  123. console.log("\n=== plugin ===\n")
  124. await import(`../packages/plugin/script/publish.ts`)
  125. const dir = new URL("..", import.meta.url).pathname
  126. process.chdir(dir)
  127. if (!Script.preview) {
  128. await $`git commit -am "release: v${Script.version}"`
  129. await $`git tag v${Script.version}`
  130. await $`git fetch origin`
  131. await $`git cherry-pick HEAD..origin/dev`.nothrow()
  132. await $`git push origin HEAD --tags --no-verify --force-with-lease`
  133. await new Promise((resolve) => setTimeout(resolve, 5_000))
  134. await $`gh release create v${Script.version} -d --title "v${Script.version}" --notes ${notes.join("\n") || "No notable changes"} ./packages/opencode/dist/*.zip ./packages/opencode/dist/*.tar.gz`
  135. const release = await $`gh release view v${Script.version} --json id,tagName`.json()
  136. if (process.env.GITHUB_OUTPUT) {
  137. await Bun.write(process.env.GITHUB_OUTPUT, `releaseId=${release.id}\ntagName=${release.tagName}\n`)
  138. }
  139. }