refactor(release): rewrite publish scripts with Effect

Refactor the detached release flow and retry-safe package publishers into Effect.gen pipelines while preserving the stacked branch behavior from the release-fix PR.
This commit is contained in:
Kit Langton 2026-04-17 22:54:35 -04:00
commit 7f4dc4a249
5 changed files with 321 additions and 231 deletions

View file

@ -1,5 +1,7 @@
#!/usr/bin/env bun
import { $ } from "bun"
import { Effect } from "effect"
import pkg from "../package.json"
import { Script } from "@opencode-ai/script"
import { fileURLToPath } from "url"
@ -7,82 +9,110 @@ import { fileURLToPath } from "url"
const dir = fileURLToPath(new URL("..", import.meta.url))
process.chdir(dir)
async function published(name: string, version: string) {
return (await $`npm view ${name}@${version} version`.nothrow()).exitCode === 0
}
const published = (name: string, version: string) =>
Effect.promise(() => $`npm view ${name}@${version} version`.nothrow()).pipe(
Effect.map((result) => result.exitCode === 0),
)
async function publish(dir: string, name: string, version: string) {
if (await published(name, version)) {
console.log(`already published ${name}@${version}`)
return
const publishPackage = (dir: string, name: string, version: string) =>
Effect.gen(function* () {
if (yield* published(name, version)) {
console.log(`already published ${name}@${version}`)
return
}
if (process.platform !== "win32") yield* Effect.promise(() => $`chmod -R 755 .`.cwd(dir))
yield* Effect.promise(() => $`bun pm pack`.cwd(dir))
yield* Effect.promise(() => $`npm publish *.tgz --access public --tag ${Script.channel}`.cwd(dir))
})
const binaryVersion = (value: unknown) => {
if (
typeof value === "object" &&
value !== null &&
"name" in value &&
typeof value.name === "string" &&
"version" in value &&
typeof value.version === "string"
) {
return value
}
if (process.platform !== "win32") await $`chmod -R 755 .`.cwd(dir)
await $`bun pm pack`.cwd(dir)
await $`npm publish *.tgz --access public --tag ${Script.channel}`.cwd(dir)
throw new Error("invalid dist package manifest")
}
const binaries: Record<string, string> = {}
for (const filepath of new Bun.Glob("*/package.json").scanSync({ cwd: "./dist" })) {
const pkg = await Bun.file(`./dist/${filepath}`).json()
binaries[pkg.name] = pkg.version
const ensureVersion = (value: unknown) => {
if (typeof value === "string") return value
throw new Error("missing dist package version")
}
console.log("binaries", binaries)
const version = Object.values(binaries)[0]
await $`mkdir -p ./dist/${pkg.name}`
await $`cp -r ./bin ./dist/${pkg.name}/bin`
await $`cp ./script/postinstall.mjs ./dist/${pkg.name}/postinstall.mjs`
await Bun.file(`./dist/${pkg.name}/LICENSE`).write(await Bun.file("../../LICENSE").text())
const program = Effect.gen(function* () {
const binaries: Record<string, string> = Object.fromEntries(
yield* Effect.promise(async () =>
Array.fromAsync(new Bun.Glob("*/package.json").scan({ cwd: "./dist" }), async (filepath) => {
const current = binaryVersion(await Bun.file(`./dist/${filepath}`).json())
return [current.name, current.version] as const
}),
),
)
console.log("binaries", binaries)
await Bun.file(`./dist/${pkg.name}/package.json`).write(
JSON.stringify(
{
name: pkg.name + "-ai",
bin: {
[pkg.name]: `./bin/${pkg.name}`,
},
scripts: {
postinstall: "bun ./postinstall.mjs || node ./postinstall.mjs",
},
version: version,
license: pkg.license,
optionalDependencies: binaries,
},
null,
2,
),
)
const version = ensureVersion(Object.values(binaries)[0])
const tasks = Object.entries(binaries).map(async ([name]) => {
await publish(`./dist/${name}`, name, binaries[name])
})
await Promise.all(tasks)
await publish(`./dist/${pkg.name}`, `${pkg.name}-ai`, version)
yield* Effect.promise(() => $`mkdir -p ./dist/${pkg.name}`)
yield* Effect.promise(() => $`cp -r ./bin ./dist/${pkg.name}/bin`)
yield* Effect.promise(() => $`cp ./script/postinstall.mjs ./dist/${pkg.name}/postinstall.mjs`)
yield* Effect.promise(async () =>
Bun.file(`./dist/${pkg.name}/LICENSE`).write(await Bun.file("../../LICENSE").text()),
)
yield* Effect.promise(() =>
Bun.write(
`./dist/${pkg.name}/package.json`,
JSON.stringify(
{
name: pkg.name + "-ai",
bin: { [pkg.name]: `./bin/${pkg.name}` },
scripts: { postinstall: "bun ./postinstall.mjs || node ./postinstall.mjs" },
version,
license: pkg.license,
optionalDependencies: binaries,
},
null,
2,
),
),
)
const image = "ghcr.io/anomalyco/opencode"
const platforms = "linux/amd64,linux/arm64"
const tags = [`${image}:${version}`, `${image}:${Script.channel}`]
const tagFlags = tags.flatMap((t) => ["-t", t])
await $`docker buildx build --platform ${platforms} ${tagFlags} --push .`
yield* Effect.all(Object.entries(binaries).map(([name, version]) => publishPackage(`./dist/${name}`, name, version)))
yield* publishPackage(`./dist/${pkg.name}`, `${pkg.name}-ai`, version)
// registries
if (!Script.preview) {
// Calculate SHA values
const arm64Sha = await $`sha256sum ./dist/opencode-linux-arm64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim())
const x64Sha = await $`sha256sum ./dist/opencode-linux-x64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim())
const macX64Sha = await $`sha256sum ./dist/opencode-darwin-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
const macArm64Sha = await $`sha256sum ./dist/opencode-darwin-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
const image = "ghcr.io/anomalyco/opencode"
const tags = [`${image}:${version}`, `${image}:${Script.channel}`]
yield* Effect.promise(
() => $`docker buildx build --platform linux/amd64,linux/arm64 ${tags.flatMap((t) => ["-t", t])} --push .`,
)
const [pkgver, _subver = ""] = Script.version.split(/(-.*)/, 2)
if (Script.preview) return
const arm64Sha = (yield* Effect.promise(() =>
$`sha256sum ./dist/opencode-linux-arm64.tar.gz | cut -d' ' -f1`.text(),
)).trim()
const x64Sha = (yield* Effect.promise(() =>
$`sha256sum ./dist/opencode-linux-x64.tar.gz | cut -d' ' -f1`.text(),
)).trim()
const macX64Sha = (yield* Effect.promise(() =>
$`sha256sum ./dist/opencode-darwin-x64.zip | cut -d' ' -f1`.text(),
)).trim()
const macArm64Sha = (yield* Effect.promise(() =>
$`sha256sum ./dist/opencode-darwin-arm64.zip | cut -d' ' -f1`.text(),
)).trim()
const [pkgver, subver = ""] = Script.version.split(/(-.*)/, 2)
// arch
const binaryPkgbuild = [
"# Maintainer: dax",
"# Maintainer: adam",
"",
"pkgname='opencode-bin'",
`pkgver=${pkgver}`,
`_subver=${_subver}`,
`_subver=${subver}`,
"options=('!debug' '!strip')",
"pkgrel=1",
"pkgdesc='The AI coding agent built for the terminal.'",
@ -95,7 +125,6 @@ if (!Script.preview) {
"",
`source_aarch64=("\${pkgname}_\${pkgver}_aarch64.tar.gz::https://github.com/anomalyco/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-arm64.tar.gz")`,
`sha256sums_aarch64=('${arm64Sha}')`,
`source_x86_64=("\${pkgname}_\${pkgver}_x86_64.tar.gz::https://github.com/anomalyco/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-x64.tar.gz")`,
`sha256sums_x86_64=('${x64Sha}')`,
"",
@ -105,37 +134,40 @@ if (!Script.preview) {
"",
].join("\n")
for (const [pkg, pkgbuild] of [["opencode-bin", binaryPkgbuild]]) {
yield* Effect.promise(async () => {
for (let i = 0; i < 30; i++) {
try {
await $`rm -rf ./dist/aur-${pkg}`
await $`git clone ssh://aur@aur.archlinux.org/${pkg}.git ./dist/aur-${pkg}`
await $`cd ./dist/aur-${pkg} && git checkout master`
await Bun.file(`./dist/aur-${pkg}/PKGBUILD`).write(pkgbuild)
await $`cd ./dist/aur-${pkg} && makepkg --printsrcinfo > .SRCINFO`
await $`cd ./dist/aur-${pkg} && git add PKGBUILD .SRCINFO`
if ((await $`cd ./dist/aur-${pkg} && git diff --cached --quiet`.nothrow()).exitCode === 0) break
await $`cd ./dist/aur-${pkg} && git commit -m "Update to v${Script.version}"`
await $`cd ./dist/aur-${pkg} && git push`
break
} catch {
continue
}
await $`rm -rf ./dist/aur-opencode-bin`
await $`git clone ssh://aur@aur.archlinux.org/opencode-bin.git ./dist/aur-opencode-bin`
await $`cd ./dist/aur-opencode-bin && git checkout master`
await Bun.write(`./dist/aur-opencode-bin/PKGBUILD`, binaryPkgbuild)
await $`cd ./dist/aur-opencode-bin && makepkg --printsrcinfo > .SRCINFO`
await $`cd ./dist/aur-opencode-bin && git add PKGBUILD .SRCINFO`
if ((await $`cd ./dist/aur-opencode-bin && git diff --cached --quiet`.nothrow()).exitCode === 0) return
await $`cd ./dist/aur-opencode-bin && git commit -m "Update to v${Script.version}"`
await $`cd ./dist/aur-opencode-bin && git push`
return
} catch {}
}
})
const token = process.env.GITHUB_TOKEN
if (!token) {
console.error("GITHUB_TOKEN is required to update homebrew tap")
process.exit(1)
}
// Homebrew formula
const homebrewFormula = [
"# typed: false",
"# frozen_string_literal: true",
"",
"# This file was generated by GoReleaser. DO NOT EDIT.",
"class Opencode < Formula",
` desc "The AI coding agent built for the terminal."`,
` homepage "https://github.com/anomalyco/opencode"`,
' desc "The AI coding agent built for the terminal."',
' homepage "https://github.com/anomalyco/opencode"',
` version "${Script.version.split("-")[0]}"`,
"",
` depends_on "ripgrep"`,
' depends_on "ripgrep"',
"",
" on_macos do",
" if Hardware::CPU.intel?",
@ -177,18 +209,15 @@ if (!Script.preview) {
"",
].join("\n")
const token = process.env.GITHUB_TOKEN
if (!token) {
console.error("GITHUB_TOKEN is required to update homebrew tap")
process.exit(1)
}
const tap = `https://x-access-token:${token}@github.com/anomalyco/homebrew-tap.git`
await $`rm -rf ./dist/homebrew-tap`
await $`git clone ${tap} ./dist/homebrew-tap`
await Bun.file("./dist/homebrew-tap/opencode.rb").write(homebrewFormula)
await $`cd ./dist/homebrew-tap && git add opencode.rb`
if ((await $`cd ./dist/homebrew-tap && git diff --cached --quiet`.nothrow()).exitCode !== 0) {
yield* Effect.promise(async () => {
await $`rm -rf ./dist/homebrew-tap`
await $`git clone https://x-access-token:${token}@github.com/anomalyco/homebrew-tap.git ./dist/homebrew-tap`
await Bun.write("./dist/homebrew-tap/opencode.rb", homebrewFormula)
await $`cd ./dist/homebrew-tap && git add opencode.rb`
if ((await $`cd ./dist/homebrew-tap && git diff --cached --quiet`.nothrow()).exitCode === 0) return
await $`cd ./dist/homebrew-tap && git commit -m "Update to v${Script.version}"`
await $`cd ./dist/homebrew-tap && git push`
}
}
})
})
await Effect.runPromise(program)

View file

@ -1,32 +1,46 @@
#!/usr/bin/env bun
import { Script } from "@opencode-ai/script"
import { $ } from "bun"
import { Effect } from "effect"
import { fileURLToPath } from "url"
const dir = fileURLToPath(new URL("..", import.meta.url))
process.chdir(dir)
async function published(name: string, version: string) {
return (await $`npm view ${name}@${version} version`.nothrow()).exitCode === 0
type PackageJson = {
name: string
version: string
exports: Record<string, string>
}
await $`bun tsc`
const pkg = await import("../package.json").then(
(m) => m.default as { name: string; version: string; exports: Record<string, string> },
)
const original = JSON.parse(JSON.stringify(pkg))
if (await published(pkg.name, pkg.version)) {
console.log(`already published ${pkg.name}@${pkg.version}`)
process.exit(0)
}
for (const [key, value] of Object.entries(pkg.exports)) {
const file = value.replace("./src/", "./dist/").replace(".ts", "")
// @ts-ignore
pkg.exports[key] = {
import: file + ".js",
types: file + ".d.ts",
const published = (name: string, version: string) =>
Effect.promise(() => $`npm view ${name}@${version} version`.nothrow()).pipe(
Effect.map((result) => result.exitCode === 0),
)
const program = Effect.gen(function* () {
yield* Effect.promise(() => $`bun tsc`)
const pkg = (yield* Effect.promise(() => import("../package.json").then((m) => m.default))) as PackageJson
if (yield* published(pkg.name, pkg.version)) {
console.log(`already published ${pkg.name}@${pkg.version}`)
return
}
}
await Bun.write("package.json", JSON.stringify(pkg, null, 2))
await $`bun pm pack && npm publish *.tgz --tag ${Script.channel} --access public`
await Bun.write("package.json", JSON.stringify(original, null, 2))
const next = {
...pkg,
exports: Object.fromEntries(
Object.entries(pkg.exports).map(([key, value]) => {
const file = value.replace("./src/", "./dist/").replace(".ts", "")
return [key, { import: file + ".js", types: file + ".d.ts" }]
}),
),
}
yield* Effect.promise(() => Bun.write("package.json", JSON.stringify(next, null, 2)))
yield* Effect.promise(() => $`bun pm pack && npm publish *.tgz --tag ${Script.channel} --access public`)
yield* Effect.promise(() => Bun.write("package.json", JSON.stringify(pkg, null, 2)))
})
await Effect.runPromise(program)

View file

@ -2,21 +2,38 @@
import { Script } from "@opencode-ai/script"
import { $ } from "bun"
import { Effect } from "effect"
import { fileURLToPath } from "url"
const dir = fileURLToPath(new URL("..", import.meta.url))
process.chdir(dir)
async function published(name: string, version: string) {
return (await $`npm view ${name}@${version} version`.nothrow()).exitCode === 0
const packageJson = (value: unknown) => {
if (
typeof value === "object" &&
value !== null &&
"name" in value &&
typeof value.name === "string" &&
"version" in value &&
typeof value.version === "string" &&
"exports" in value &&
typeof value.exports === "object" &&
value.exports !== null
) {
return {
name: value.name,
version: value.version,
exports: value.exports,
}
}
throw new Error("invalid sdk package manifest")
}
const pkg = (await import("../package.json").then((m) => m.default)) as {
name: string
version: string
exports: Record<string, unknown>
}
const original = JSON.parse(JSON.stringify(pkg))
const published = (name: string, version: string) =>
Effect.promise(() => $`npm view ${name}@${version} version`.nothrow()).pipe(
Effect.map((result) => result.exitCode === 0),
)
function transformExports(exports: Record<string, unknown>) {
return Object.fromEntries(
Object.entries(exports).map(([key, value]) => {
@ -24,19 +41,28 @@ function transformExports(exports: Record<string, unknown>) {
const file = value.replace("./src/", "./dist/").replace(".ts", "")
return [key, { import: file + ".js", types: file + ".d.ts" }]
}
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
return [key, transformExports(value)]
}
if (typeof value === "object" && value !== null && !Array.isArray(value)) return [key, transformExports(value)]
return [key, value]
}),
)
}
if (await published(pkg.name, pkg.version)) {
console.log(`already published ${pkg.name}@${pkg.version}`)
process.exit(0)
}
pkg.exports = transformExports(pkg.exports)
await Bun.write("package.json", JSON.stringify(pkg, null, 2))
await $`bun pm pack`
await $`npm publish *.tgz --tag ${Script.channel} --access public`
await Bun.write("package.json", JSON.stringify(original, null, 2))
const program = Effect.gen(function* () {
const pkg = packageJson(yield* Effect.promise(() => import("../package.json").then((m) => m.default)))
if (yield* published(pkg.name, pkg.version)) {
console.log(`already published ${pkg.name}@${pkg.version}`)
return
}
const next = {
...pkg,
exports: transformExports(pkg.exports),
}
yield* Effect.promise(() => Bun.write("package.json", JSON.stringify(next, null, 2)))
yield* Effect.promise(() => $`bun pm pack`)
yield* Effect.promise(() => $`npm publish *.tgz --tag ${Script.channel} --access public`)
yield* Effect.promise(() => Bun.write("package.json", JSON.stringify(pkg, null, 2)))
})
await Effect.runPromise(program)