mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-09 03:03:44 +03:00
75 lines
2.4 KiB
TypeScript
75 lines
2.4 KiB
TypeScript
import { $ } from "bun"
|
|
import semver from "semver"
|
|
import path from "path"
|
|
|
|
const rootPkgPath = path.resolve(import.meta.dir, "../../../package.json")
|
|
const rootPkg = await Bun.file(rootPkgPath).json()
|
|
const expectedBunVersion = rootPkg.packageManager?.split("@")[1]
|
|
|
|
if (!expectedBunVersion) {
|
|
throw new Error("packageManager field not found in root package.json")
|
|
}
|
|
|
|
// relax version requirement
|
|
const expectedBunVersionRange = `^${expectedBunVersion}`
|
|
|
|
if (!semver.satisfies(process.versions.bun, expectedBunVersionRange)) {
|
|
throw new Error(`This script requires bun@${expectedBunVersionRange}, but you are using bun@${process.versions.bun}`)
|
|
}
|
|
|
|
const env = {
|
|
OPENCODE_CHANNEL: process.env["OPENCODE_CHANNEL"],
|
|
OPENCODE_VERSION: process.env["OPENCODE_VERSION"],
|
|
}
|
|
const CHANNEL = await (async () => {
|
|
if (env.OPENCODE_CHANNEL) return env.OPENCODE_CHANNEL
|
|
if (env.OPENCODE_VERSION && !env.OPENCODE_VERSION.startsWith("0.0.0-")) return "latest"
|
|
const branch = await $`git branch --show-current`.text().then((x) => x.trim())
|
|
if (!branch) return "detached"
|
|
const channel = branch.replace(/[^a-zA-Z0-9._-]/g, "-")
|
|
if (/^[a-zA-Z0-9]/.test(channel)) return channel
|
|
return `branch-${channel}`
|
|
})()
|
|
const IS_PREVIEW = CHANNEL !== "latest"
|
|
|
|
// The release workflow's plan job owns release versions; builds only read them.
|
|
const VERSION = (() => {
|
|
if (env.OPENCODE_VERSION) return env.OPENCODE_VERSION
|
|
if (!IS_PREVIEW) throw new Error("OPENCODE_VERSION is required for latest-channel builds")
|
|
return `0.0.0-${CHANNEL}-${previewBuildNumber()}`
|
|
})()
|
|
|
|
function previewBuildNumber() {
|
|
const runNumber = process.env["GITHUB_RUN_NUMBER"]
|
|
if (!runNumber) return new Date().toISOString().slice(0, 16).replace(/[-:T]/g, "")
|
|
const runAttempt = process.env["GITHUB_RUN_ATTEMPT"]
|
|
if (runAttempt && runAttempt !== "1") return `${runNumber}.${runAttempt}`
|
|
return runNumber
|
|
}
|
|
|
|
const bot = ["actions-user", "opencode", "opencode-agent[bot]"]
|
|
const teamPath = path.resolve(import.meta.dir, "../../../.github/TEAM_MEMBERS")
|
|
const team = [
|
|
...(await Bun.file(teamPath)
|
|
.text()
|
|
.then((x) => x.split(/\r?\n/).map((x) => x.trim()))
|
|
.then((x) => x.filter((x) => x && !x.startsWith("#")))),
|
|
...bot,
|
|
]
|
|
|
|
export const Script = {
|
|
get channel() {
|
|
return CHANNEL
|
|
},
|
|
get version() {
|
|
return VERSION
|
|
},
|
|
get preview() {
|
|
return IS_PREVIEW
|
|
},
|
|
get team() {
|
|
return team
|
|
},
|
|
}
|
|
console.log(`opencode script`, JSON.stringify(Script, null, 2))
|