update-models.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import path from "path"
  4. import os from "os"
  5. import { ZenData } from "../src/model"
  6. const root = path.resolve(process.cwd(), "..", "..", "..")
  7. const models = await $`bun sst secret list`.cwd(root).text()
  8. // read the line starting with "ZEN_MODELS"
  9. const lines = models.split("\n")
  10. const oldValue1 = lines.find((line) => line.startsWith("ZEN_MODELS1"))?.split("=")[1]
  11. const oldValue2 = lines.find((line) => line.startsWith("ZEN_MODELS2"))?.split("=")[1]
  12. const oldValue3 = lines.find((line) => line.startsWith("ZEN_MODELS3"))?.split("=")[1]
  13. const oldValue4 = lines.find((line) => line.startsWith("ZEN_MODELS4"))?.split("=")[1]
  14. const oldValue5 = lines.find((line) => line.startsWith("ZEN_MODELS5"))?.split("=")[1]
  15. const oldValue6 = lines.find((line) => line.startsWith("ZEN_MODELS6"))?.split("=")[1]
  16. const oldValue7 = lines.find((line) => line.startsWith("ZEN_MODELS7"))?.split("=")[1]
  17. if (!oldValue1) throw new Error("ZEN_MODELS1 not found")
  18. if (!oldValue2) throw new Error("ZEN_MODELS2 not found")
  19. if (!oldValue3) throw new Error("ZEN_MODELS3 not found")
  20. if (!oldValue4) throw new Error("ZEN_MODELS4 not found")
  21. if (!oldValue5) throw new Error("ZEN_MODELS5 not found")
  22. if (!oldValue6) throw new Error("ZEN_MODELS6 not found")
  23. if (!oldValue7) throw new Error("ZEN_MODELS7 not found")
  24. // store the prettified json to a temp file
  25. const filename = `models-${Date.now()}.json`
  26. const tempFile = Bun.file(path.join(os.tmpdir(), filename))
  27. await tempFile.write(
  28. JSON.stringify(
  29. JSON.parse(oldValue1 + oldValue2 + oldValue3 + oldValue4 + oldValue5 + oldValue6 + oldValue7),
  30. null,
  31. 2,
  32. ),
  33. )
  34. console.log("tempFile", tempFile.name)
  35. // open temp file in vim and read the file on close
  36. await $`vim ${tempFile.name}`
  37. const newValue = JSON.stringify(JSON.parse(await tempFile.text()))
  38. ZenData.validate(JSON.parse(newValue))
  39. // update the secret
  40. const chunk = Math.ceil(newValue.length / 7)
  41. const newValue1 = newValue.slice(0, chunk)
  42. const newValue2 = newValue.slice(chunk, chunk * 2)
  43. const newValue3 = newValue.slice(chunk * 2, chunk * 3)
  44. const newValue4 = newValue.slice(chunk * 3, chunk * 4)
  45. const newValue5 = newValue.slice(chunk * 4, chunk * 5)
  46. const newValue6 = newValue.slice(chunk * 5, chunk * 6)
  47. const newValue7 = newValue.slice(chunk * 6)
  48. await $`bun sst secret set ZEN_MODELS1 ${newValue1}`
  49. await $`bun sst secret set ZEN_MODELS2 ${newValue2}`
  50. await $`bun sst secret set ZEN_MODELS3 ${newValue3}`
  51. await $`bun sst secret set ZEN_MODELS4 ${newValue4}`
  52. await $`bun sst secret set ZEN_MODELS5 ${newValue5}`
  53. await $`bun sst secret set ZEN_MODELS6 ${newValue6}`
  54. await $`bun sst secret set ZEN_MODELS7 ${newValue7}`