this repo has no description
at v1.2.0 1.6 kB view raw
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 moonlightFS.writeFileString( 22 configPath, 23 JSON.stringify(config, null, 2) 24 ); 25 } catch (e) { 26 logger.error("Failed to write config", e); 27 } 28} 29 30export async function readConfig(): Promise<Config> { 31 webPreload: { 32 return moonlightNode.config; 33 } 34 35 const configPath = await getConfigPath(); 36 if (!(await moonlightFS.exists(configPath))) { 37 await writeConfig(defaultConfig); 38 return defaultConfig; 39 } else { 40 try { 41 let config: Config = JSON.parse( 42 await moonlightFS.readFileString(configPath) 43 ); 44 // Assign the default values if they don't exist (newly added) 45 config = { ...defaultConfig, ...config }; 46 await writeConfig(config); 47 48 return config; 49 } catch (e) { 50 logger.error("Failed to read config, falling back to defaults", e); 51 // We don't want to write the default config here - if a user is manually 52 // editing their config and messes it up, we'll delete it all instead of 53 // letting them fix it 54 return defaultConfig; 55 } 56 } 57}