diff --git a/src/process/config.js b/src/process/config.js new file mode 100644 index 0000000..b424123 --- /dev/null +++ b/src/process/config.js @@ -0,0 +1,63 @@ +import { app } from "electron"; +import { readFile, writeFile } from "node:fs/promises"; +import { join } from "node:path"; + +/** + * @typedef {Object} Config + */ + +/** + * @param {string} configName + * + * @returns {Promise} + */ +export async function getConfig(configName) { + const configFilePath = getConfigFilePath(configName); + + try { + const configData = await readFile(configFilePath, "utf8"); + const config = configData && JSON.parse(configData); + + return config; + } catch (error) { + console.error("[ERROR] [config:get] Failed to read the file."); + } +} + +/** + * @param {string} configName + * @param {Partial} config + * + * @returns + */ +export function updateConfig(configName, config) { + const configFilePath = getConfigFilePath(configName); + const currentConfig = getConfig(configName); + + try { + const configJSON = JSON.stringify( + { + ...currentConfig, + ...config, + }, + null, + "\t", + ); + writeFile(configFilePath, configJSON, "utf8"); + + console.log("[INFO] [config:update] The file has been saved!"); + } catch (error) { + console.error("[ERROR] [config:update] Failed to save the file."); + } +} + +/** + * @param {string} configName + * + * @returns + */ +function getConfigFilePath(configName) { + const configDir = app.getPath("userData"); + + return join(configDir, `${configName}.json`); +}