From a86378e7268490056c9be8fe6b9c9d0ba1f9ef89 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Tue, 23 Apr 2024 16:45:14 +0530 Subject: [PATCH] feat(config): add `updateConfig` method --- src/config.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/config.ts b/src/config.ts index 055e37f..7929ba7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -20,6 +20,10 @@ interface Config { }; } +type RecursivePartial = { + [P in keyof T]?: RecursivePartial; +}; + const loadConfig = () => toml.parse( fs.readFileSync(path.join(__dirname, `../${configFileName}`), 'utf-8'), @@ -40,3 +44,28 @@ export const getOpenaiApiKey = () => loadConfig().API_KEYS.OPENAI; export const getSearxngApiEndpoint = () => loadConfig().API_ENDPOINTS.SEARXNG; export const getOllamaApiEndpoint = () => loadConfig().API_ENDPOINTS.OLLAMA; + +export const updateConfig = (config: RecursivePartial) => { + const currentConfig = loadConfig(); + + for (const key in currentConfig) { + /* if (currentConfig[key] && !config[key]) { + config[key] = currentConfig[key]; + } */ + + if (currentConfig[key] && typeof currentConfig[key] === 'object') { + for (const nestedKey in currentConfig[key]) { + if (currentConfig[key][nestedKey] && !config[key][nestedKey]) { + config[key][nestedKey] = currentConfig[key][nestedKey]; + } + } + } else if (currentConfig[key] && !config[key]) { + config[key] = currentConfig[key]; + } + } + + fs.writeFileSync( + path.join(__dirname, `../${configFileName}`), + toml.stringify(config), + ); +};