Skip to content

Commit 24fb9b1

Browse files
authored
fix: stop rewriting dev during release publish (#22982)
1 parent 3573019 commit 24fb9b1

5 files changed

Lines changed: 120 additions & 50 deletions

File tree

packages/opencode/script/publish.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ import { fileURLToPath } from "url"
77
const dir = fileURLToPath(new URL("..", import.meta.url))
88
process.chdir(dir)
99

10+
async function published(name: string, version: string) {
11+
return (await $`npm view ${name}@${version} version`.nothrow()).exitCode === 0
12+
}
13+
14+
async function publish(dir: string, name: string, version: string) {
15+
if (await published(name, version)) {
16+
console.log(`already published ${name}@${version}`)
17+
return
18+
}
19+
if (process.platform !== "win32") await $`chmod -R 755 .`.cwd(dir)
20+
await $`bun pm pack`.cwd(dir)
21+
await $`npm publish *.tgz --access public --tag ${Script.channel}`.cwd(dir)
22+
}
23+
1024
const binaries: Record<string, string> = {}
1125
for (const filepath of new Bun.Glob("*/package.json").scanSync({ cwd: "./dist" })) {
1226
const pkg = await Bun.file(`./dist/${filepath}`).json()
@@ -40,14 +54,10 @@ await Bun.file(`./dist/${pkg.name}/package.json`).write(
4054
)
4155

4256
const tasks = Object.entries(binaries).map(async ([name]) => {
43-
if (process.platform !== "win32") {
44-
await $`chmod -R 755 .`.cwd(`./dist/${name}`)
45-
}
46-
await $`bun pm pack`.cwd(`./dist/${name}`)
47-
await $`npm publish *.tgz --access public --tag ${Script.channel}`.cwd(`./dist/${name}`)
57+
await publish(`./dist/${name}`, name, binaries[name])
4858
})
4959
await Promise.all(tasks)
50-
await $`cd ./dist/${pkg.name} && bun pm pack && npm publish *.tgz --access public --tag ${Script.channel}`
60+
await publish(`./dist/${pkg.name}`, `${pkg.name}-ai`, version)
5161

5262
const image = "ghcr.io/anomalyco/opencode"
5363
const platforms = "linux/amd64,linux/arm64"
@@ -104,6 +114,7 @@ if (!Script.preview) {
104114
await Bun.file(`./dist/aur-${pkg}/PKGBUILD`).write(pkgbuild)
105115
await $`cd ./dist/aur-${pkg} && makepkg --printsrcinfo > .SRCINFO`
106116
await $`cd ./dist/aur-${pkg} && git add PKGBUILD .SRCINFO`
117+
if ((await $`cd ./dist/aur-${pkg} && git diff --cached --quiet`.nothrow()).exitCode === 0) break
107118
await $`cd ./dist/aur-${pkg} && git commit -m "Update to v${Script.version}"`
108119
await $`cd ./dist/aur-${pkg} && git push`
109120
break
@@ -176,6 +187,8 @@ if (!Script.preview) {
176187
await $`git clone ${tap} ./dist/homebrew-tap`
177188
await Bun.file("./dist/homebrew-tap/opencode.rb").write(homebrewFormula)
178189
await $`cd ./dist/homebrew-tap && git add opencode.rb`
179-
await $`cd ./dist/homebrew-tap && git commit -m "Update to v${Script.version}"`
180-
await $`cd ./dist/homebrew-tap && git push`
190+
if ((await $`cd ./dist/homebrew-tap && git diff --cached --quiet`.nothrow()).exitCode !== 0) {
191+
await $`cd ./dist/homebrew-tap && git commit -m "Update to v${Script.version}"`
192+
await $`cd ./dist/homebrew-tap && git push`
193+
}
181194
}

packages/plugin/script/publish.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ import { fileURLToPath } from "url"
66
const dir = fileURLToPath(new URL("..", import.meta.url))
77
process.chdir(dir)
88

9+
async function published(name: string, version: string) {
10+
return (await $`npm view ${name}@${version} version`.nothrow()).exitCode === 0
11+
}
12+
913
await $`bun tsc`
10-
const pkg = await import("../package.json").then((m) => m.default)
14+
const pkg = await import("../package.json").then(
15+
(m) => m.default as { name: string; version: string; exports: Record<string, string> },
16+
)
1117
const original = JSON.parse(JSON.stringify(pkg))
18+
if (await published(pkg.name, pkg.version)) {
19+
console.log(`already published ${pkg.name}@${pkg.version}`)
20+
process.exit(0)
21+
}
1222
for (const [key, value] of Object.entries(pkg.exports)) {
1323
const file = value.replace("./src/", "./dist/").replace(".ts", "")
1424
// @ts-ignore

packages/sdk/js/script/publish.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,35 @@ import { fileURLToPath } from "url"
77
const dir = fileURLToPath(new URL("..", import.meta.url))
88
process.chdir(dir)
99

10+
async function published(name: string, version: string) {
11+
return (await $`npm view ${name}@${version} version`.nothrow()).exitCode === 0
12+
}
13+
1014
const pkg = (await import("../package.json").then((m) => m.default)) as {
11-
exports: Record<string, string | object>
15+
name: string
16+
version: string
17+
exports: Record<string, unknown>
1218
}
1319
const original = JSON.parse(JSON.stringify(pkg))
14-
function transformExports(exports: Record<string, string | object>) {
15-
for (const [key, value] of Object.entries(exports)) {
16-
if (typeof value === "object" && value !== null) {
17-
transformExports(value as Record<string, string | object>)
18-
} else if (typeof value === "string") {
19-
const file = value.replace("./src/", "./dist/").replace(".ts", "")
20-
exports[key] = {
21-
import: file + ".js",
22-
types: file + ".d.ts",
20+
function transformExports(exports: Record<string, unknown>) {
21+
return Object.fromEntries(
22+
Object.entries(exports).map(([key, value]) => {
23+
if (typeof value === "string") {
24+
const file = value.replace("./src/", "./dist/").replace(".ts", "")
25+
return [key, { import: file + ".js", types: file + ".d.ts" }]
2326
}
24-
}
25-
}
27+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
28+
return [key, transformExports(value)]
29+
}
30+
return [key, value]
31+
}),
32+
)
33+
}
34+
if (await published(pkg.name, pkg.version)) {
35+
console.log(`already published ${pkg.name}@${pkg.version}`)
36+
process.exit(0)
2637
}
27-
transformExports(pkg.exports)
38+
pkg.exports = transformExports(pkg.exports)
2839
await Bun.write("package.json", JSON.stringify(pkg, null, 2))
2940
await $`bun pm pack`
3041
await $`npm publish *.tgz --tag ${Script.channel} --access public`

script/publish.ts

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,58 @@ import { fileURLToPath } from "url"
66

77
console.log("=== publishing ===\n")
88

9+
const tag = `v${Script.version}`
10+
911
const pkgjsons = await Array.fromAsync(
1012
new Bun.Glob("**/package.json").scan({
1113
absolute: true,
1214
}),
1315
).then((arr) => arr.filter((x) => !x.includes("node_modules") && !x.includes("dist")))
1416

15-
for (const file of pkgjsons) {
16-
let pkg = await Bun.file(file).text()
17-
pkg = pkg.replaceAll(/"version": "[^"]+"/g, `"version": "${Script.version}"`)
18-
console.log("updated:", file)
19-
await Bun.file(file).write(pkg)
20-
}
21-
2217
const extensionToml = fileURLToPath(new URL("../packages/extensions/zed/extension.toml", import.meta.url))
23-
let toml = await Bun.file(extensionToml).text()
24-
toml = toml.replace(/^version = "[^"]+"/m, `version = "${Script.version}"`)
25-
toml = toml.replaceAll(/releases\/download\/v[^/]+\//g, `releases/download/v${Script.version}/`)
26-
console.log("updated:", extensionToml)
27-
await Bun.file(extensionToml).write(toml)
2818

29-
await $`bun install`
30-
await import(`../packages/sdk/js/script/build.ts`)
19+
async function hasChanges() {
20+
return (await $`git diff --quiet && git diff --cached --quiet`.nothrow()).exitCode !== 0
21+
}
3122

32-
if (Script.release) {
33-
if (!Script.preview) {
34-
await $`git commit -am "release: v${Script.version}"`
35-
await $`git tag v${Script.version}`
36-
await $`git fetch origin`
37-
await $`git cherry-pick HEAD..origin/dev`.nothrow()
38-
await $`git push origin HEAD --tags --no-verify --force-with-lease`
39-
await new Promise((resolve) => setTimeout(resolve, 5_000))
23+
async function releaseTagExists() {
24+
return (await $`git rev-parse -q --verify refs/tags/${tag}`.nothrow()).exitCode === 0
25+
}
26+
27+
async function prepareReleaseFiles() {
28+
for (const file of pkgjsons) {
29+
let pkg = await Bun.file(file).text()
30+
pkg = pkg.replaceAll(/"version": "[^"]+"/g, `"version": "${Script.version}"`)
31+
console.log("updated:", file)
32+
await Bun.file(file).write(pkg)
4033
}
4134

42-
await import(`../packages/desktop/scripts/finalize-latest-json.ts`)
43-
await import(`../packages/desktop-electron/scripts/finalize-latest-yml.ts`)
35+
let toml = await Bun.file(extensionToml).text()
36+
toml = toml.replace(/^version = "[^"]+"/m, `version = "${Script.version}"`)
37+
toml = toml.replaceAll(/releases\/download\/v[^/]+\//g, `releases/download/v${Script.version}/`)
38+
console.log("updated:", extensionToml)
39+
await Bun.file(extensionToml).write(toml)
40+
41+
await $`bun install`
42+
await $`./packages/sdk/js/script/build.ts`
43+
}
44+
45+
if (Script.release && !Script.preview) {
46+
await $`git fetch origin --tags`
47+
await $`git switch --detach`
48+
}
4449

45-
await $`gh release edit v${Script.version} --draft=false --repo ${process.env.GH_REPO}`
50+
await prepareReleaseFiles()
51+
52+
if (Script.release && !Script.preview) {
53+
if (await releaseTagExists()) {
54+
console.log(`release tag ${tag} already exists, skipping tag creation`)
55+
} else {
56+
await $`git commit -am "release: ${tag}"`
57+
await $`git tag ${tag}`
58+
await $`git push origin refs/tags/${tag} --no-verify`
59+
await new Promise((resolve) => setTimeout(resolve, 5_000))
60+
}
4661
}
4762

4863
console.log("\n=== cli ===\n")
@@ -54,5 +69,26 @@ await import(`../packages/sdk/js/script/publish.ts`)
5469
console.log("\n=== plugin ===\n")
5570
await import(`../packages/plugin/script/publish.ts`)
5671

72+
if (Script.release) {
73+
await import(`../packages/desktop/scripts/finalize-latest-json.ts`)
74+
await import(`../packages/desktop-electron/scripts/finalize-latest-yml.ts`)
75+
}
76+
77+
if (Script.release && !Script.preview) {
78+
await $`git fetch origin`
79+
await $`git checkout -B dev origin/dev`
80+
await prepareReleaseFiles()
81+
if (await hasChanges()) {
82+
await $`git commit -am "sync release versions for v${Script.version}"`
83+
await $`git push origin HEAD:dev --no-verify`
84+
} else {
85+
console.log(`dev already synced for ${tag}`)
86+
}
87+
}
88+
89+
if (Script.release) {
90+
await $`gh release edit ${tag} --draft=false --repo ${process.env.GH_REPO}`
91+
}
92+
5793
const dir = fileURLToPath(new URL("..", import.meta.url))
5894
process.chdir(dir)

script/version.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { Script } from "@opencode-ai/script"
44
import { $ } from "bun"
55

66
const output = [`version=${Script.version}`]
7+
const sha = process.env.GITHUB_SHA ?? (await $`git rev-parse HEAD`.text()).trim()
78

89
if (!Script.preview) {
9-
const sha = process.env.GITHUB_SHA ?? (await $`git rev-parse HEAD`.text()).trim()
1010
await $`bun script/changelog.ts --to ${sha}`.cwd(process.cwd())
1111
const file = `${process.cwd()}/UPCOMING_CHANGELOG.md`
1212
const body = await Bun.file(file)
@@ -15,12 +15,12 @@ if (!Script.preview) {
1515
const dir = process.env.RUNNER_TEMP ?? "/tmp"
1616
const notesFile = `${dir}/opencode-release-notes.txt`
1717
await Bun.write(notesFile, body)
18-
await $`gh release create v${Script.version} -d --title "v${Script.version}" --notes-file ${notesFile}`
18+
await $`gh release create v${Script.version} -d --target ${sha} --title "v${Script.version}" --notes-file ${notesFile}`
1919
const release = await $`gh release view v${Script.version} --json tagName,databaseId`.json()
2020
output.push(`release=${release.databaseId}`)
2121
output.push(`tag=${release.tagName}`)
2222
} else if (Script.channel === "beta") {
23-
await $`gh release create v${Script.version} -d --title "v${Script.version}" --repo ${process.env.GH_REPO}`
23+
await $`gh release create v${Script.version} -d --target ${sha} --title "v${Script.version}" --repo ${process.env.GH_REPO}`
2424
const release =
2525
await $`gh release view v${Script.version} --json tagName,databaseId --repo ${process.env.GH_REPO}`.json()
2626
output.push(`release=${release.databaseId}`)

0 commit comments

Comments
 (0)