publish.ts 3.1 KB

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