update-models.ts 2.1 KB

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