this repo has no description
1import { Config } from "@moonlight-mod/types";
2import { getConfigPath } from "./util/data";
3import * as constants from "@moonlight-mod/types/constants";
4import Logger from "./util/logger";
5
6const logger = new Logger("core/config");
7
8const defaultConfig: Config = {
9 extensions: {
10 moonbase: true,
11 disableSentry: true,
12 noTrack: true,
13 noHideToken: true
14 },
15 repositories: [constants.mainRepo]
16};
17
18export async function writeConfig(config: Config) {
19 try {
20 const configPath = await getConfigPath();
21 await moonlightNodeSandboxed.fs.writeFileString(configPath, JSON.stringify(config, null, 2));
22 } catch (e) {
23 logger.error("Failed to write config", e);
24 }
25}
26
27export async function readConfig(): Promise<Config> {
28 webPreload: {
29 return moonlightNode.config;
30 }
31
32 const configPath = await getConfigPath();
33 if (!(await moonlightNodeSandboxed.fs.exists(configPath))) {
34 await writeConfig(defaultConfig);
35 return defaultConfig;
36 } else {
37 try {
38 let config: Config = JSON.parse(await moonlightNodeSandboxed.fs.readFileString(configPath));
39 // Assign the default values if they don't exist (newly added)
40 config = { ...defaultConfig, ...config };
41 await writeConfig(config);
42
43 return config;
44 } catch (e) {
45 logger.error("Failed to read config, falling back to defaults", e);
46 // We don't want to write the default config here - if a user is manually
47 // editing their config and messes it up, we'll delete it all instead of
48 // letting them fix it
49 return defaultConfig;
50 }
51 }
52}