publish.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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: "kimi-k2",
  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. IMPORTANT: ONLY return a bulleted list of changes, do not include any other information. Do not include a preamble like "Based on my analysis..."
  45. <example>
  46. - Added ability to @ mention agents
  47. - Fixed a bug where the TUI would render improperly on some terminals
  48. </example>
  49. `,
  50. },
  51. ],
  52. },
  53. })
  54. .then((x) => x.data?.parts?.find((y) => y.type === "text")?.text)
  55. for (const line of raw?.split("\n") ?? []) {
  56. if (line.startsWith("- ")) {
  57. notes.push(line)
  58. }
  59. }
  60. console.log("---- Generated Changelog ----")
  61. console.log(notes.join("\n"))
  62. console.log("-----------------------------")
  63. opencode.server.close()
  64. }
  65. const pkgjsons = await Array.fromAsync(
  66. new Bun.Glob("**/package.json").scan({
  67. absolute: true,
  68. }),
  69. ).then((arr) => arr.filter((x) => !x.includes("node_modules") && !x.includes("dist")))
  70. for (const file of pkgjsons) {
  71. let pkg = await Bun.file(file).text()
  72. pkg = pkg.replaceAll(/"version": "[^"]+"/g, `"version": "${Script.version}"`)
  73. console.log("updated:", file)
  74. await Bun.file(file).write(pkg)
  75. }
  76. const extensionToml = new URL("../packages/extensions/zed/extension.toml", import.meta.url).pathname
  77. let toml = await Bun.file(extensionToml).text()
  78. toml = toml.replace(/^version = "[^"]+"/m, `version = "${Script.version}"`)
  79. toml = toml.replaceAll(/releases\/download\/v[^/]+\//g, `releases/download/v${Script.version}/`)
  80. console.log("updated:", extensionToml)
  81. await Bun.file(extensionToml).write(toml)
  82. await $`bun install`
  83. console.log("\n=== opencode ===\n")
  84. await import(`../packages/opencode/script/publish.ts`)
  85. console.log("\n=== sdk ===\n")
  86. await import(`../packages/sdk/js/script/publish.ts`)
  87. console.log("\n=== plugin ===\n")
  88. await import(`../packages/plugin/script/publish.ts`)
  89. const dir = new URL("..", import.meta.url).pathname
  90. process.chdir(dir)
  91. if (!Script.preview) {
  92. await $`git commit -am "release: v${Script.version}"`
  93. await $`git tag v${Script.version}`
  94. await $`git fetch origin`
  95. await $`git cherry-pick HEAD..origin/dev`.nothrow()
  96. await $`git push origin HEAD --tags --no-verify --force-with-lease`
  97. await new Promise((resolve) => setTimeout(resolve, 5_000))
  98. await $`gh release create v${Script.version} --title "v${Script.version}" --notes ${notes.join("\n") ?? "No notable changes"} ./packages/opencode/dist/*.zip`
  99. }