publish.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import { createOpencodeClient, createOpencodeServer } 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:)/i))
  19. .join("\n")
  20. createOpencodeServer({
  21. port: 4096,
  22. }).catch(() => {})
  23. await new Promise((resolve) => setTimeout(resolve, 1_000))
  24. const opencode = {
  25. client: createOpencodeClient({
  26. baseUrl: `http://127.0.0.1:4096`,
  27. }),
  28. }
  29. const session = await opencode.client.session.create()
  30. console.log("generating changelog since " + previous)
  31. const raw = await opencode.client.session
  32. .prompt({
  33. path: {
  34. id: session.data!.id,
  35. },
  36. body: {
  37. model: {
  38. providerID: "opencode",
  39. modelID: "kimi-k2",
  40. },
  41. parts: [
  42. {
  43. type: "text",
  44. text: `
  45. Analyze these commits and generate a changelog of all notable user facing changes.
  46. Commits between ${previous} and HEAD:
  47. ${commits}
  48. - Do NOT make general statements about "improvements", be very specific about what was changed.
  49. - Do NOT include any information about code changes if they do not affect the user facing changes.
  50. - 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.
  51. IMPORTANT: ONLY return a bulleted list of changes, do not include any other information. Do not include a preamble like "Based on my analysis..."
  52. <example>
  53. - Added ability to @ mention agents
  54. - Fixed a bug where the TUI would render improperly on some terminals
  55. </example>
  56. `,
  57. },
  58. ],
  59. },
  60. })
  61. .then((x) => x.data?.parts?.find((y) => y.type === "text")?.text)
  62. for (const line of raw?.split("\n") ?? []) {
  63. if (line.startsWith("- ")) {
  64. notes.push(line)
  65. }
  66. }
  67. console.log(notes)
  68. }
  69. const pkgjsons = await Array.fromAsync(
  70. new Bun.Glob("**/package.json").scan({
  71. absolute: true,
  72. }),
  73. ).then((arr) => arr.filter((x) => !x.includes("node_modules") && !x.includes("dist")))
  74. for (const file of pkgjsons) {
  75. let pkg = await Bun.file(file).text()
  76. pkg = pkg.replaceAll(/"version": "[^"]+"/g, `"version": "${Script.version}"`)
  77. console.log("updated:", file)
  78. await Bun.file(file).write(pkg)
  79. }
  80. await $`bun install`
  81. console.log("\n=== opencode ===\n")
  82. await import(`../packages/opencode/script/publish.ts`)
  83. console.log("\n=== sdk ===\n")
  84. await import(`../packages/sdk/js/script/publish.ts`)
  85. console.log("\n=== plugin ===\n")
  86. await import(`../packages/plugin/script/publish.ts`)
  87. const dir = new URL("..", import.meta.url).pathname
  88. process.chdir(dir)
  89. if (!Script.preview) {
  90. await $`git commit -am "release: v${Script.version}"`
  91. await $`git tag v${Script.version}`
  92. await $`git fetch origin`
  93. await $`git cherry-pick HEAD..origin/dev`.nothrow()
  94. await $`git push origin HEAD --tags --no-verify --force-with-lease`
  95. await new Promise((resolve) => setTimeout(resolve, 5_000))
  96. await $`gh release create v${Script.version} --title "v${Script.version}" --notes ${notes.join("\n") ?? "No notable changes"} ./packages/opencode/dist/*.zip`
  97. }