#!/usr/bin/env bun import { Script } from "@opencode/script" import { NpmLibraries } from "@opencode/script/npm" import { $ } from "bun" import { fileURLToPath } from "url" import { packageJsons, prepareReleaseFiles } from "./release-files" console.log("=== publishing ===\n") const dir = fileURLToPath(new URL("..", import.meta.url)) process.chdir(dir) const tag = `v${Script.version}` // Beta dogfoods the desktop app. The CLI and SDK packages ship from dev. if (Script.channel === "beta") { console.log("\n=== desktop ===\n") await $`bun ./packages/desktop/scripts/publish.ts` process.exit(0) } const pkgjsons = await packageJsons() async function writeChangelog() { const notes = process.env["OPENCODE_RELEASE_NOTES"]?.trim() if (!notes) return const file = Bun.file("CHANGELOG.md") const text = await file.text() const lines = text.split("\n") if (lines.some((line) => line === `## ${tag}` || line.startsWith(`## ${tag} `))) return const index = lines.findIndex((line) => line.startsWith("## ")) const head = (index === -1 ? lines : lines.slice(0, index)).join("\n").trimEnd() const rest = index === -1 ? "" : lines.slice(index).join("\n") const entry = `## ${tag} — ${new Date().toISOString().slice(0, 10)}\n\n${notes}\n` await file.write(`${head}\n\n${entry}${rest ? `\n${rest}` : ""}`) } // Check pack-npm's tarballs before anything is published. if (NpmLibraries.tarballs) await NpmLibraries.verify(NpmLibraries.tarballs) if (Script.release && !Script.preview) { await $`git fetch origin --tags` await $`git switch --detach` } await prepareReleaseFiles(pkgjsons) if (Script.release) await $`bun ./packages/desktop/scripts/publish.ts --dry-run` // The CLI publishes right after client, so a failing later library cannot hold back a dev preview. for (const name of NpmLibraries.names) { console.log(`\n=== ${name} ===\n`) await $`bun ./packages/${name}/script/publish.ts` if (name !== "client") continue console.log("\n=== cli ===\n") await $`bun ./packages/cli/script/publish.ts` } if (Script.release && !Script.preview) { if ((await $`git diff --quiet`.nothrow()).exitCode !== 0) await $`git commit -am "release: ${tag}"` await $`git tag -d ${tag}`.nothrow() await $`git tag ${tag}` await $`git push origin refs/tags/${tag} --force-with-lease --no-verify` await new Promise((resolve) => setTimeout(resolve, 5_000)) await $`git fetch origin` await $`git checkout -B dev origin/dev` await prepareReleaseFiles(pkgjsons) await writeChangelog() if ((await $`git diff --quiet`.nothrow()).exitCode !== 0) { // The release already published this code; a push-triggered dev publish of a version bump is wasted work. await $`git commit -am ${`sync release versions for ${tag} [skip ci]`}` await $`git push origin HEAD:dev --no-verify` } } if (Script.release) { console.log("\n=== desktop ===\n") await $`bun ./packages/desktop/scripts/publish.ts` } // WinGet submission is paused until our packages are accepted into microsoft/winget-pkgs. // if (Script.release && Script.channel === "latest") { // console.log("\n=== winget ===\n") // // Everything else is published by now, and WinGet validation can reject a submission for reasons unrelated to the release. // if ((await $`bun ./script/publish-winget.ts`.nothrow()).exitCode !== 0) // console.log("::warning::WinGet submission failed; see the winget step output above") // }