upgrade-opentui.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/keymap", "@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 setVersion = (cur: string) => {
  16. if (cur === "catalog:" || cur.startsWith("workspace:")) return cur
  17. if (cur.startsWith(">=")) return `>=${ver}`
  18. if (cur.startsWith("^")) return `^${ver}`
  19. if (cur.startsWith("~")) return `~${ver}`
  20. return ver
  21. }
  22. const editDeps = (obj: unknown) => {
  23. if (!obj || typeof obj !== "object") return false
  24. const map = obj as Record<string, unknown>
  25. return keys
  26. .map((key) => {
  27. const cur = map[key]
  28. if (typeof cur !== "string") return false
  29. const next = setVersion(cur)
  30. if (next === cur) return false
  31. map[key] = next
  32. return true
  33. })
  34. .some(Boolean)
  35. }
  36. const editCatalog = (obj: unknown) => {
  37. if (!obj || typeof obj !== "object") return false
  38. const map = obj as Record<string, unknown>
  39. return keys
  40. .map((key) => {
  41. const cur = map[key]
  42. if (typeof cur !== "string" || cur === ver) return false
  43. map[key] = ver
  44. return true
  45. })
  46. .some(Boolean)
  47. }
  48. const out = (
  49. await Promise.all(
  50. files.map(async (rel) => {
  51. const file = path.join(root, rel)
  52. const txt = await Bun.file(file).text()
  53. const json = JSON.parse(txt)
  54. const hit = [
  55. editCatalog(json.workspaces?.catalog),
  56. editDeps(json.dependencies),
  57. editDeps(json.devDependencies),
  58. editDeps(json.peerDependencies),
  59. ].some(Boolean)
  60. if (!hit) return null
  61. await Bun.write(file, `${JSON.stringify(json, null, 2)}\n`)
  62. return rel
  63. }),
  64. )
  65. ).filter((item): item is string => item !== null)
  66. if (out.length === 0) {
  67. console.log("No opentui deps found")
  68. process.exit(0)
  69. }
  70. console.log(`Updated opentui to ${ver} in:`)
  71. for (const file of out) {
  72. console.log(`- ${file}`)
  73. }