update-black.ts 985 B

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import path from "path"
  4. import os from "os"
  5. import { BlackData } from "../src/black"
  6. const root = path.resolve(process.cwd(), "..", "..", "..")
  7. const secrets = await $`bun sst secret list`.cwd(root).text()
  8. // read the line starting with "ZEN_BLACK"
  9. const lines = secrets.split("\n")
  10. const oldValue = lines.find((line) => line.startsWith("ZEN_BLACK"))?.split("=")[1]
  11. if (!oldValue) throw new Error("ZEN_BLACK not found")
  12. // store the prettified json to a temp file
  13. const filename = `black-${Date.now()}.json`
  14. const tempFile = Bun.file(path.join(os.tmpdir(), filename))
  15. await tempFile.write(JSON.stringify(JSON.parse(oldValue), null, 2))
  16. console.log("tempFile", tempFile.name)
  17. // open temp file in vim and read the file on close
  18. await $`vim ${tempFile.name}`
  19. const newValue = JSON.stringify(JSON.parse(await tempFile.text()))
  20. BlackData.validate(JSON.parse(newValue))
  21. // update the secret
  22. await $`bun sst secret set ZEN_BLACK ${newValue}`