publish.ts 3.5 KB

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