1.0.0-beta2.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.2...");
  6. // Determine which config file exists
  7. const filePaths = [configFilePath1, configFilePath2];
  8. let filePath = "";
  9. for (const path of filePaths) {
  10. if (fs.existsSync(path)) {
  11. filePath = path;
  12. break;
  13. }
  14. }
  15. if (!filePath) {
  16. throw new Error(
  17. `No config file found (expected config.yml or config.yaml).`
  18. );
  19. }
  20. // Read and parse the YAML file
  21. let rawConfig: any;
  22. const fileContents = fs.readFileSync(filePath, "utf8");
  23. rawConfig = yaml.load(fileContents);
  24. // Validate the structure
  25. if (!rawConfig.app || !rawConfig.app.base_url) {
  26. throw new Error(`Invalid config file: app.base_url is missing.`);
  27. }
  28. // Move base_url to dashboard_url and calculate base_domain
  29. const baseUrl = rawConfig.app.base_url;
  30. rawConfig.app.dashboard_url = baseUrl;
  31. rawConfig.app.base_domain = getBaseDomain(baseUrl);
  32. // Remove the old base_url
  33. delete rawConfig.app.base_url;
  34. // Write the updated YAML back to the file
  35. const updatedYaml = yaml.dump(rawConfig);
  36. fs.writeFileSync(filePath, updatedYaml, "utf8");
  37. console.log("Done.");
  38. }
  39. function getBaseDomain(url: string): string {
  40. const newUrl = new URL(url);
  41. const hostname = newUrl.hostname;
  42. const parts = hostname.split(".");
  43. if (parts.length <= 2) {
  44. return parts.join(".");
  45. }
  46. return parts.slice(-2).join(".");
  47. }