publish.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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`.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. }
  66. const pkgjsons = await Array.fromAsync(
  67. new Bun.Glob("**/package.json").scan({
  68. absolute: true,
  69. }),
  70. ).then((arr) => arr.filter((x) => !x.includes("node_modules") && !x.includes("dist")))
  71. for (const file of pkgjsons) {
  72. let pkg = await Bun.file(file).text()
  73. pkg = pkg.replaceAll(/"version": "[^"]+"/g, `"version": "${Script.version}"`)
  74. console.log("updated:", file)
  75. await Bun.file(file).write(pkg)
  76. }
  77. const extensionToml = new URL("../packages/extensions/zed/extension.toml", import.meta.url).pathname
  78. let toml = await Bun.file(extensionToml).text()
  79. toml = toml.replace(/^version = "[^"]+"/m, `version = "${Script.version}"`)
  80. toml = toml.replaceAll(/releases\/download\/v[^/]+\//g, `releases/download/v${Script.version}/`)
  81. console.log("updated:", extensionToml)
  82. await Bun.file(extensionToml).write(toml)
  83. await $`bun install`
  84. console.log("\n=== opencode ===\n")
  85. await import(`../packages/opencode/script/publish.ts`)
  86. console.log("\n=== sdk ===\n")
  87. await import(`../packages/sdk/js/script/publish.ts`)
  88. console.log("\n=== plugin ===\n")
  89. await import(`../packages/plugin/script/publish.ts`)
  90. const dir = new URL("..", import.meta.url).pathname
  91. process.chdir(dir)
  92. if (!Script.preview) {
  93. await $`git commit -am "release: v${Script.version}"`
  94. await $`git tag v${Script.version}`
  95. await $`git fetch origin`
  96. await $`git cherry-pick HEAD..origin/dev`.nothrow()
  97. await $`git push origin HEAD --tags --no-verify --force-with-lease`
  98. await new Promise((resolve) => setTimeout(resolve, 5_000))
  99. await $`gh release create v${Script.version} --title "v${Script.version}" --notes ${notes.join("\n") ?? "No notable changes"} ./packages/opencode/dist/*.zip ./packages/opencode/dist/*.tar.gz`
  100. }