index.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { $ } from "bun"
  2. import path from "path"
  3. const rootPkgPath = path.resolve(import.meta.dir, "../../../package.json")
  4. const rootPkg = await Bun.file(rootPkgPath).json()
  5. const expectedBunVersion = rootPkg.packageManager?.split("@")[1]
  6. if (!expectedBunVersion) {
  7. throw new Error("packageManager field not found in root package.json")
  8. }
  9. if (process.versions.bun !== expectedBunVersion) {
  10. throw new Error(
  11. `This script requires bun@${expectedBunVersion}, but you are using bun@${process.versions.bun}`,
  12. )
  13. }
  14. const CHANNEL =
  15. process.env["OPENCODE_CHANNEL"] ??
  16. (await $`git branch --show-current`.text().then((x) => x.trim()))
  17. const IS_PREVIEW = CHANNEL !== "latest"
  18. const VERSION = await (async () => {
  19. if (process.env["OPENCODE_VERSION"]) return process.env["OPENCODE_VERSION"]
  20. if (IS_PREVIEW)
  21. return `0.0.0-${CHANNEL}-${new Date().toISOString().slice(0, 16).replace(/[-:T]/g, "")}`
  22. const version = await fetch("https://registry.npmjs.org/opencode-ai/latest")
  23. .then((res) => {
  24. if (!res.ok) throw new Error(res.statusText)
  25. return res.json()
  26. })
  27. .then((data: any) => data.version)
  28. const [major, minor, patch] = version.split(".").map((x: string) => Number(x) || 0)
  29. const t = process.env["OPENCODE_BUMP"]?.toLowerCase()
  30. if (t === "major") return `${major + 1}.0.0`
  31. if (t === "minor") return `${major}.${minor + 1}.0`
  32. return `${major}.${minor}.${patch + 1}`
  33. })()
  34. export const Script = {
  35. get channel() {
  36. return CHANNEL
  37. },
  38. get version() {
  39. return VERSION
  40. },
  41. get preview() {
  42. return IS_PREVIEW
  43. },
  44. }
  45. console.log(`opencode script`, JSON.stringify(Script, null, 2))