update-models.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. if (!oldValue1) throw new Error("ZEN_MODELS1 not found")
  17. if (!oldValue2) throw new Error("ZEN_MODELS2 not found")
  18. if (!oldValue3) throw new Error("ZEN_MODELS3 not found")
  19. if (!oldValue4) throw new Error("ZEN_MODELS4 not found")
  20. if (!oldValue5) throw new Error("ZEN_MODELS5 not found")
  21. if (!oldValue6) throw new Error("ZEN_MODELS6 not found")
  22. // store the prettified json to a temp file
  23. const filename = `models-${Date.now()}.json`
  24. const tempFile = Bun.file(path.join(os.tmpdir(), filename))
  25. await tempFile.write(
  26. JSON.stringify(JSON.parse(oldValue1 + oldValue2 + oldValue3 + oldValue4 + oldValue5 + oldValue6), null, 2),
  27. )
  28. console.log("tempFile", tempFile.name)
  29. // open temp file in vim and read the file on close
  30. await $`vim ${tempFile.name}`
  31. const newValue = JSON.stringify(JSON.parse(await tempFile.text()))
  32. ZenData.validate(JSON.parse(newValue))
  33. // update the secret
  34. const chunk = Math.ceil(newValue.length / 6)
  35. const newValue1 = newValue.slice(0, chunk)
  36. const newValue2 = newValue.slice(chunk, chunk * 2)
  37. const newValue3 = newValue.slice(chunk * 2, chunk * 3)
  38. const newValue4 = newValue.slice(chunk * 3, chunk * 4)
  39. const newValue5 = newValue.slice(chunk * 4, chunk * 5)
  40. const newValue6 = newValue.slice(chunk * 5)
  41. await $`bun sst secret set ZEN_MODELS1 ${newValue1}`
  42. await $`bun sst secret set ZEN_MODELS2 ${newValue2}`
  43. await $`bun sst secret set ZEN_MODELS3 ${newValue3}`
  44. await $`bun sst secret set ZEN_MODELS4 ${newValue4}`
  45. await $`bun sst secret set ZEN_MODELS5 ${newValue5}`
  46. await $`bun sst secret set ZEN_MODELS6 ${newValue6}`