1.0.0-beta6.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { configFilePath1, configFilePath2 } from "@server/lib/consts";
  2. import fs from "fs";
  3. import yaml from "js-yaml";
  4. export default async function migration() {
  5. console.log("Running setup script 1.0.0-beta.6...");
  6. try {
  7. // Determine which config file exists
  8. const filePaths = [configFilePath1, configFilePath2];
  9. let filePath = "";
  10. for (const path of filePaths) {
  11. if (fs.existsSync(path)) {
  12. filePath = path;
  13. break;
  14. }
  15. }
  16. if (!filePath) {
  17. throw new Error(
  18. `No config file found (expected config.yml or config.yaml).`
  19. );
  20. }
  21. // Read and parse the YAML file
  22. let rawConfig: any;
  23. const fileContents = fs.readFileSync(filePath, "utf8");
  24. rawConfig = yaml.load(fileContents);
  25. // Validate the structure
  26. if (!rawConfig.server) {
  27. throw new Error(`Invalid config file: server is missing.`);
  28. }
  29. // Update the config
  30. rawConfig.server.cors = {
  31. origins: [rawConfig.app.dashboard_url],
  32. methods: ["GET", "POST", "PUT", "DELETE", "PATCH"],
  33. headers: ["X-CSRF-Token", "Content-Type"],
  34. credentials: false
  35. };
  36. // Write the updated YAML back to the file
  37. const updatedYaml = yaml.dump(rawConfig);
  38. fs.writeFileSync(filePath, updatedYaml, "utf8");
  39. } catch (error) {
  40. console.log("We were unable to add CORS to your config file. Please add it manually.")
  41. console.error(error)
  42. }
  43. console.log("Done.");
  44. }