-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigManager.js
More file actions
78 lines (69 loc) · 2 KB
/
configManager.js
File metadata and controls
78 lines (69 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const { shell } = require("electron");
const fs = require("fs");
const path = require("path");
const os = require("os");
const appName = "CryptoWidget";
const configDir = path.join(os.homedir(), "Documents", appName);
const configFile = path.join(configDir, "config.json");
const defaultConfig = {
version: "1.0.1",
position:{
type: "top-right", // "custom", "top-right", "bottom-right", "bottom-left", "top-left"
x: 0,
y: 0
},
alwaysOnTop: false,
cryptoList: [{
coin: "BTC",
currency: "USDT",
average: 1
}]
};
function createConfigFile() {
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
if (!fs.existsSync(configFile)) {
fs.writeFileSync(configFile, JSON.stringify(defaultConfig, null, 2), "utf-8");
}
}
/**
* Loads the config.json file. If it doesn't exist, it creates a new one with default values.
* @returns {object} Parsed JSON configuration
*/
function loadConfigFile() {
if (!fs.existsSync(configFile)) {
createConfigFile();
}
try {
const data = fs.readFileSync(configFile, "utf-8");
var jsonData = JSON.parse(data);
if (!jsonData.version || jsonData.version !== defaultConfig.version) {
fs.writeFileSync(configFile, JSON.stringify(defaultConfig, null, 2), "utf-8");
return defaultConfig;
}
return jsonData;
} catch (error) {
console.error("Error loading config file:", error);
return defaultConfig;
}
}
/**
* Updates the config.json file.
* @param {object} config - The configuration object to save.
*/
function updateConfigFile(config) {
fs.writeFileSync(configFile, JSON.stringify(config, null, 2), "utf-8");
}
/**
* Opens the config.json file in the default text editor.
*/
function openConfigFile() {
createConfigFile()
shell.openPath(configFile).then((error) => {
if (error) {
console.error("Failed to open config file:", error);
}
});
}
module.exports = { createConfigFile, loadConfigFile, openConfigFile, updateConfigFile };