config.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { ConfigManager } from "@sv443-network/userutils";
  2. import { featInfo } from "./features/index";
  3. import { FeatureConfig } from "./types";
  4. import { info, log } from "./utils";
  5. /** If this number is incremented, the features object data will be migrated to the new format */
  6. const formatVersion = 1;
  7. export const defaultConfig = (Object.keys(featInfo) as (keyof typeof featInfo)[])
  8. .reduce<Partial<FeatureConfig>>((acc, key) => {
  9. acc[key] = featInfo[key].default as unknown as undefined;
  10. return acc;
  11. }, {}) as FeatureConfig;
  12. const cfgMgr = new ConfigManager({
  13. id: "bytm-config",
  14. formatVersion,
  15. defaultConfig,
  16. });
  17. /** Initializes the ConfigManager instance and loads persistent data into memory */
  18. export async function initConfig() {
  19. const data = await cfgMgr.loadData();
  20. log(`Initialized ConfigManager (format version = ${cfgMgr.formatVersion})`);
  21. return data;
  22. }
  23. /** Returns the current feature config from the in-memory cache */
  24. export function getFeatures() {
  25. return cfgMgr.getData();
  26. }
  27. /** Saves the feature config synchronously to the in-memory cache and asynchronously to the persistent storage */
  28. export async function saveFeatures(featureConf: FeatureConfig) {
  29. await cfgMgr.setData(featureConf);
  30. info("Saved new feature config:", featureConf);
  31. }
  32. /** Saves the default feature config synchronously to the in-memory cache and asynchronously to persistent storage */
  33. export async function setDefaultFeatures() {
  34. await cfgMgr.saveDefaultData();
  35. info("Reset feature config to its default value");
  36. }