publish.ts 3.2 KB

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