publish.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. const tree = await $`git add . && git write-tree`.text().then((x) => x.trim())
  65. for (const file of pkgjsons) {
  66. let pkg = await Bun.file(file).text()
  67. pkg = pkg.replaceAll(/"version": "[^"]+"/g, `"version": "${Script.version}"`)
  68. console.log("updated:", file)
  69. await Bun.file(file).write(pkg)
  70. }
  71. await $`bun install`
  72. console.log("\n=== opencode ===\n")
  73. await import(`../packages/opencode/script/publish.ts`)
  74. console.log("\n=== sdk ===\n")
  75. await import(`../packages/sdk/js/script/publish.ts`)
  76. console.log("\n=== plugin ===\n")
  77. await import(`../packages/plugin/script/publish.ts`)
  78. const dir = new URL("..", import.meta.url).pathname
  79. process.chdir(dir)
  80. if (!Script.preview) {
  81. await $`git commit -am "release: v${Script.version}"`
  82. await $`git tag v${Script.version}`
  83. await $`git fetch origin`
  84. await $`git cherry-pick HEAD..origin/dev`.nothrow()
  85. await $`git push origin HEAD --tags --no-verify --force`
  86. await $`gh release create v${Script.version} --title "v${Script.version}" --notes ${notes.join("\n") ?? "No notable changes"} ./packages/opencode/dist/*.zip`
  87. }
  88. if (Script.preview) {
  89. await $`git checkout -b snapshot-${Script.version}`
  90. await $`git commit --allow-empty -m "Snapshot release v${Script.version}"`
  91. await $`git tag v${Script.version}`
  92. await $`git push origin v${Script.version} --no-verify`
  93. await $`git checkout dev`
  94. await $`git branch -D snapshot-${Script.version}`
  95. for (const file of pkgjsons) {
  96. await $`git checkout ${tree} ${file}`
  97. }
  98. }