Skip to content

Commit

Permalink
feat: add config support
Browse files Browse the repository at this point in the history
  • Loading branch information
Belar committed Jan 4, 2025
1 parent 92146a2 commit 7a11674
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/process/config.js
Original file line number Diff line number Diff line change
@@ -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<Config | undefined>}
*/
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>} 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`);
}

0 comments on commit 7a11674

Please sign in to comment.