upgrade-opentui.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env bun
  2. import path from "node:path"
  3. const raw = process.argv[2]
  4. if (!raw) {
  5. console.error("Usage: bun run script/upgrade-opentui.ts <version>")
  6. process.exit(1)
  7. }
  8. const ver = raw.replace(/^v/, "")
  9. const root = path.resolve(import.meta.dir, "../../..")
  10. const skip = new Set([".git", ".opencode", ".turbo", "dist", "node_modules"])
  11. const keys = ["@opentui/core", "@opentui/solid"] as const
  12. const files = (await Array.fromAsync(new Bun.Glob("**/package.json").scan({ cwd: root }))).filter(
  13. (file) => !file.split("/").some((part) => skip.has(part)),
  14. )
  15. const set = (cur: string) => {
  16. if (cur.startsWith(">=")) return `>=${ver}`
  17. if (cur.startsWith("^")) return `^${ver}`
  18. if (cur.startsWith("~")) return `~${ver}`
  19. return ver
  20. }
  21. const edit = (obj: unknown) => {
  22. if (!obj || typeof obj !== "object") return false
  23. const map = obj as Record<string, unknown>
  24. return keys
  25. .map((key) => {
  26. const cur = map[key]
  27. if (typeof cur !== "string") return false
  28. const next = set(cur)
  29. if (next === cur) return false
  30. map[key] = next
  31. return true
  32. })
  33. .some(Boolean)
  34. }
  35. const out = (
  36. await Promise.all(
  37. files.map(async (rel) => {
  38. const file = path.join(root, rel)
  39. const txt = await Bun.file(file).text()
  40. const json = JSON.parse(txt)
  41. const hit = [json.dependencies, json.devDependencies, json.peerDependencies].map(edit).some(Boolean)
  42. if (!hit) return null
  43. await Bun.write(file, `${JSON.stringify(json, null, 2)}\n`)
  44. return rel
  45. }),
  46. )
  47. ).filter((item): item is string => item !== null)
  48. if (out.length === 0) {
  49. console.log("No opentui deps found")
  50. process.exit(0)
  51. }
  52. console.log(`Updated opentui to ${ver} in:`)
  53. for (const file of out) {
  54. console.log(`- ${file}`)
  55. }