index.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { $ } from "bun"
  2. import semver from "semver"
  3. import path from "path"
  4. const rootPkgPath = path.resolve(import.meta.dir, "../../../package.json")
  5. const rootPkg = await Bun.file(rootPkgPath).json()
  6. const expectedBunVersion = rootPkg.packageManager?.split("@")[1]
  7. if (!expectedBunVersion) {
  8. throw new Error("packageManager field not found in root package.json")
  9. }
  10. // relax version requirement
  11. const expectedBunVersionRange = `^${expectedBunVersion}`
  12. if (!semver.satisfies(process.versions.bun, expectedBunVersionRange)) {
  13. throw new Error(`This script requires bun@${expectedBunVersionRange}, but you are using bun@${process.versions.bun}`)
  14. }
  15. const env = {
  16. OPENCODE_CHANNEL: process.env["OPENCODE_CHANNEL"],
  17. OPENCODE_BUMP: process.env["OPENCODE_BUMP"],
  18. OPENCODE_VERSION: process.env["OPENCODE_VERSION"],
  19. OPENCODE_RELEASE: process.env["OPENCODE_RELEASE"],
  20. }
  21. const CHANNEL = await (async () => {
  22. if (env.OPENCODE_CHANNEL) return env.OPENCODE_CHANNEL
  23. if (env.OPENCODE_BUMP) return "latest"
  24. if (env.OPENCODE_VERSION && !env.OPENCODE_VERSION.startsWith("0.0.0-")) return "latest"
  25. return await $`git branch --show-current`.text().then((x) => x.trim())
  26. })()
  27. const IS_PREVIEW = CHANNEL !== "latest"
  28. const VERSION = await (async () => {
  29. if (env.OPENCODE_VERSION) return env.OPENCODE_VERSION
  30. if (IS_PREVIEW) return `0.0.0-${CHANNEL}-${new Date().toISOString().slice(0, 16).replace(/[-:T]/g, "")}`
  31. const version = await fetch("https://registry.npmjs.org/opencode-ai/latest")
  32. .then((res) => {
  33. if (!res.ok) throw new Error(res.statusText)
  34. return res.json()
  35. })
  36. .then((data: any) => data.version)
  37. const [major, minor, patch] = version.split(".").map((x: string) => Number(x) || 0)
  38. const t = env.OPENCODE_BUMP?.toLowerCase()
  39. if (t === "major") return `${major + 1}.0.0`
  40. if (t === "minor") return `${major}.${minor + 1}.0`
  41. return `${major}.${minor}.${patch + 1}`
  42. })()
  43. const bot = ["actions-user", "opencode", "opencode-agent[bot]"]
  44. const teamPath = path.resolve(import.meta.dir, "../../../.github/TEAM_MEMBERS")
  45. const team = [
  46. ...(await Bun.file(teamPath)
  47. .text()
  48. .then((x) => x.split(/\r?\n/).map((x) => x.trim()))
  49. .then((x) => x.filter((x) => x && !x.startsWith("#")))),
  50. ...bot,
  51. ]
  52. export const Script = {
  53. get channel() {
  54. return CHANNEL
  55. },
  56. get version() {
  57. return VERSION
  58. },
  59. get preview() {
  60. return IS_PREVIEW
  61. },
  62. get release(): boolean {
  63. return !!env.OPENCODE_RELEASE
  64. },
  65. get team() {
  66. return team
  67. },
  68. }
  69. console.log(`opencode script`, JSON.stringify(Script, null, 2))