publish.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import { createOpencode } from "@opencode-ai/sdk"
  4. if (process.versions.bun !== "1.3.0") {
  5. throw new Error("This script requires [email protected]")
  6. }
  7. const notes = [] as string[]
  8. console.log("=== publishing ===\n")
  9. const snapshot = process.env["OPENCODE_SNAPSHOT"] === "true"
  10. const version = await (async () => {
  11. if (snapshot) return `0.0.0-${new Date().toISOString().slice(0, 16).replace(/[-:T]/g, "")}`
  12. if (process.env["OPENCODE_VERSION"]) return process.env["OPENCODE_VERSION"]
  13. const npmVersion = await fetch("https://registry.npmjs.org/opencode-ai/latest")
  14. .then((res) => {
  15. if (!res.ok) throw new Error(res.statusText)
  16. return res.json()
  17. })
  18. .then((data: any) => data.version)
  19. const [major, minor, patch] = npmVersion.split(".").map((x: string) => Number(x) || 0)
  20. const t = process.env["OPENCODE_BUMP"]?.toLowerCase()
  21. if (t === "major") return `${major + 1}.0.0`
  22. if (t === "minor") return `${major}.${minor + 1}.0`
  23. return `${major}.${minor}.${patch + 1}`
  24. })()
  25. process.env["OPENCODE_VERSION"] = version
  26. console.log("version:", version)
  27. if (!snapshot) {
  28. const previous = await fetch("https://registry.npmjs.org/opencode-ai/latest")
  29. .then((res) => {
  30. if (!res.ok) throw new Error(res.statusText)
  31. return res.json()
  32. })
  33. .then((data: any) => data.version)
  34. const opencode = await createOpencode()
  35. const session = await opencode.client.session.create()
  36. console.log("generating changelog since " + previous)
  37. const raw = await opencode.client.session
  38. .prompt({
  39. path: {
  40. id: session.data!.id,
  41. },
  42. body: {
  43. model: {
  44. providerID: "opencode",
  45. modelID: "kimi-k2",
  46. },
  47. parts: [
  48. {
  49. type: "text",
  50. text: `
  51. Analyze the commits between ${previous} and HEAD.
  52. We care about changes to
  53. - packages/opencode
  54. - packages/sdk
  55. - packages/plugin
  56. We do not care about anything else
  57. Return a changelog of all notable user facing changes.
  58. - Do NOT make general statements about "improvements", be very specific about what was changed.
  59. - Do NOT include any information about code changes if they do not affect the user facing changes.
  60. IMPORTANT: ONLY return a bulleted list of changes, do not include any other information. Do not include a preamble like "Based on my analysis..."
  61. <example>
  62. - Added ability to @ mention agents
  63. - Fixed a bug where the TUI would render improperly on some terminals
  64. </example>
  65. `,
  66. },
  67. ],
  68. },
  69. })
  70. .then((x) => x.data?.parts?.find((y) => y.type === "text")?.text)
  71. for (const line of raw?.split("\n") ?? []) {
  72. if (line.startsWith("- ")) {
  73. notes.push(line)
  74. }
  75. }
  76. console.log(notes)
  77. opencode.server.close()
  78. }
  79. const pkgjsons = await Array.fromAsync(
  80. new Bun.Glob("**/package.json").scan({
  81. absolute: true,
  82. }),
  83. ).then((arr) => arr.filter((x) => !x.includes("node_modules") && !x.includes("dist")))
  84. const tree = await $`git add . && git write-tree`.text().then((x) => x.trim())
  85. for (const file of pkgjsons) {
  86. let pkg = await Bun.file(file).text()
  87. pkg = pkg.replaceAll(/"version": "[^"]+"/g, `"version": "${version}"`)
  88. console.log("updated:", file)
  89. await Bun.file(file).write(pkg)
  90. }
  91. await $`bun install`
  92. console.log("\n=== opencode ===\n")
  93. await import(`../packages/opencode/script/publish.ts`)
  94. console.log("\n=== sdk ===\n")
  95. await import(`../packages/sdk/js/script/publish.ts`)
  96. console.log("\n=== plugin ===\n")
  97. await import(`../packages/plugin/script/publish.ts`)
  98. const dir = new URL("..", import.meta.url).pathname
  99. process.chdir(dir)
  100. if (!snapshot) {
  101. await $`git commit -am "release: v${version}"`
  102. await $`git tag v${version}`
  103. await $`git fetch origin`
  104. await $`git cherry-pick HEAD..origin/dev`.nothrow()
  105. await $`git push origin HEAD --tags --no-verify --force`
  106. await $`gh release create v${version} --title "v${version}" --notes ${notes.join("\n") ?? "No notable changes"} ./packages/opencode/dist/*.zip`
  107. }
  108. if (snapshot) {
  109. await $`git checkout -b snapshot-${version}`
  110. await $`git commit --allow-empty -m "Snapshot release v${version}"`
  111. await $`git tag v${version}`
  112. await $`git push origin v${version} --no-verify`
  113. await $`git checkout dev`
  114. await $`git branch -D snapshot-${version}`
  115. for (const file of pkgjsons) {
  116. await $`git checkout ${tree} ${file}`
  117. }
  118. }