1.0.0-beta5.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { APP_PATH, configFilePath1, configFilePath2 } from "@server/lib/consts";
  2. import fs from "fs";
  3. import yaml from "js-yaml";
  4. import path from "path";
  5. import { z } from "zod";
  6. import { fromZodError } from "zod-validation-error";
  7. export default async function migration() {
  8. console.log("Running setup script 1.0.0-beta.5...");
  9. // Determine which config file exists
  10. const filePaths = [configFilePath1, configFilePath2];
  11. let filePath = "";
  12. for (const path of filePaths) {
  13. if (fs.existsSync(path)) {
  14. filePath = path;
  15. break;
  16. }
  17. }
  18. if (!filePath) {
  19. throw new Error(
  20. `No config file found (expected config.yml or config.yaml).`
  21. );
  22. }
  23. // Read and parse the YAML file
  24. let rawConfig: any;
  25. const fileContents = fs.readFileSync(filePath, "utf8");
  26. rawConfig = yaml.load(fileContents);
  27. // Validate the structure
  28. if (!rawConfig.server) {
  29. throw new Error(`Invalid config file: server is missing.`);
  30. }
  31. // Update the config
  32. rawConfig.server.resource_access_token_param = "p_token";
  33. // Write the updated YAML back to the file
  34. const updatedYaml = yaml.dump(rawConfig);
  35. fs.writeFileSync(filePath, updatedYaml, "utf8");
  36. // then try to update badger in traefik config
  37. try {
  38. const traefikPath = path.join(
  39. APP_PATH,
  40. "traefik",
  41. "traefik_config.yml"
  42. );
  43. // read the traefik file
  44. // look for the badger middleware
  45. // set the version to v1.0.0-beta.2
  46. /*
  47. experimental:
  48. plugins:
  49. badger:
  50. moduleName: "github.com/fosrl/badger"
  51. version: "v1.0.0-beta.2"
  52. */
  53. const schema = z.object({
  54. experimental: z.object({
  55. plugins: z.object({
  56. badger: z.object({
  57. moduleName: z.string(),
  58. version: z.string()
  59. })
  60. })
  61. })
  62. });
  63. const traefikFileContents = fs.readFileSync(traefikPath, "utf8");
  64. const traefikConfig = yaml.load(traefikFileContents) as any;
  65. const parsedConfig = schema.safeParse(traefikConfig);
  66. if (!parsedConfig.success) {
  67. throw new Error(fromZodError(parsedConfig.error).toString());
  68. }
  69. traefikConfig.experimental.plugins.badger.version = "v1.0.0-beta.2";
  70. const updatedTraefikYaml = yaml.dump(traefikConfig);
  71. fs.writeFileSync(traefikPath, updatedTraefikYaml, "utf8");
  72. console.log(
  73. "Updated the version of Badger in your Traefik configuration to v1.0.0-beta.2."
  74. );
  75. } catch (e) {
  76. console.log(
  77. "We were unable to update the version of Badger in your Traefik configuration. Please update it manually."
  78. );
  79. console.error(e);
  80. }
  81. console.log("Done.");
  82. }