pull-models.ts 996 B

123456789101112131415161718192021222324252627282930313233
  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 stage = process.argv[2]
  7. if (!stage) throw new Error("Stage is required")
  8. const root = path.resolve(process.cwd(), "..", "..", "..")
  9. const PARTS = 10
  10. // read the secret
  11. const ret = await $`bun sst secret list --stage ${stage}`.cwd(root).text()
  12. const lines = ret.split("\n")
  13. const values = Array.from({ length: PARTS }, (_, i) => {
  14. const value = lines
  15. .find((line) => line.startsWith(`ZEN_MODELS${i + 1}=`))
  16. ?.split("=")
  17. .slice(1)
  18. .join("=")
  19. if (!value) throw new Error(`ZEN_MODELS${i + 1} not found`)
  20. return value
  21. })
  22. // validate value
  23. ZenData.validate(JSON.parse(values.join("")))
  24. // update the secret
  25. const envFile = Bun.file(path.join(os.tmpdir(), `models-${Date.now()}.env`))
  26. await envFile.write(values.map((v, i) => `ZEN_MODELS${i + 1}="${v.replace(/"/g, '\\"')}"`).join("\n"))
  27. await $`bun sst secret load ${envFile.name}`.cwd(root)