feat(config): add `updateConfig` method

This commit is contained in:
ItzCrazyKns 2024-04-23 16:45:14 +05:30
parent fd65af53c3
commit a86378e726
No known key found for this signature in database
GPG Key ID: 8162927C7CCE3065
1 changed files with 29 additions and 0 deletions

View File

@ -20,6 +20,10 @@ interface Config {
};
}
type RecursivePartial<T> = {
[P in keyof T]?: RecursivePartial<T[P]>;
};
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<Config>) => {
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),
);
};