update-models.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. const PARTS = 10
  9. // read the line starting with "ZEN_MODELS"
  10. const lines = models.split("\n")
  11. const oldValues = Array.from({ length: PARTS }, (_, i) => {
  12. const value = lines
  13. .find((line) => line.startsWith(`ZEN_MODELS${i + 1}=`))
  14. ?.split("=")
  15. .slice(1)
  16. .join("=")
  17. // TODO
  18. //if (!value) throw new Error(`ZEN_MODELS${i + 1} not found`)
  19. //return value
  20. return value ?? ""
  21. })
  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(JSON.stringify(JSON.parse(oldValues.join("")), null, 2))
  26. console.log("tempFile", tempFile.name)
  27. // open temp file in vim and read the file on close
  28. await $`vim ${tempFile.name}`
  29. const newValue = JSON.stringify(JSON.parse(await tempFile.text()))
  30. ZenData.validate(JSON.parse(newValue))
  31. // update the secret
  32. const chunk = Math.ceil(newValue.length / PARTS)
  33. const newValues = Array.from({ length: PARTS }, (_, i) =>
  34. newValue.slice(chunk * i, i === PARTS - 1 ? undefined : chunk * (i + 1)),
  35. )
  36. const envFile = Bun.file(path.join(os.tmpdir(), `models-${Date.now()}.env`))
  37. await envFile.write(newValues.map((v, i) => `ZEN_MODELS${i + 1}=${v}`).join("\n"))
  38. await $`bun sst secret load ${envFile.name}`.cwd(root)