Skip to content

Commit b83e3db

Browse files
committed
fix: config
1 parent dbdf816 commit b83e3db

File tree

1 file changed

+104
-32
lines changed

1 file changed

+104
-32
lines changed

src/js/core/config.js

Lines changed: 104 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ const yaml = require('js-yaml');
44
const { app } = require('@electron/remote');
55
const path = require('path');
66

7-
class config {
7+
class Config {
88
static instance = null;
99

1010
constructor() {
11-
if (config.instance) {
12-
return config.instance;
11+
if (Config.instance) {
12+
return Config.instance;
1313
}
1414

1515
this.default_config = {};
@@ -23,15 +23,15 @@ class config {
2323
this.readConfigYaml();
2424
this.checkConfigVersion();
2525

26-
config.instance = this;
26+
Config.instance = this;
2727
}
2828

2929
static getInstance() {
30-
if (!config.instance) {
31-
new config();
30+
if (!Config.instance) {
31+
new Config();
3232
}
3333

34-
return config.instance;
34+
return Config.instance;
3535
}
3636

3737
checkConfigExists() {
@@ -50,47 +50,116 @@ class config {
5050
this.config = yaml.load(raw);
5151
}
5252

53+
writeConfig(config = this.config) {
54+
logger.debug('Writing config:', JSON.stringify(config, null, 2));
55+
56+
const lines = [];
57+
let configContent = fs.readFileSync(this.defaultDir, 'utf8');
58+
const templateLines = configContent.split('\n');
59+
let currentKey = '';
60+
61+
for (const line of templateLines) {
62+
const keyMatch = line.match(/^(\w+):|^([\w-]+):/);
63+
const indentedKeyMatch = line.match(/^\s+(\w+):|^\s+([\w-]+):/);
64+
65+
if (keyMatch) {
66+
currentKey = keyMatch[1] || keyMatch[2];
67+
const value = config[currentKey];
68+
logger.debug(`Processing key: ${currentKey}, value:`, value);
69+
70+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
71+
lines.push(`${currentKey}:`);
72+
}
73+
else {
74+
lines.push(`${currentKey}: ${value}`);
75+
}
76+
}
77+
else if (indentedKeyMatch && currentKey) {
78+
const subKey = indentedKeyMatch[1] || indentedKeyMatch[2];
79+
if (
80+
config[currentKey]
81+
&& typeof config[currentKey][subKey] !== 'undefined'
82+
) {
83+
const value = config[currentKey][subKey];
84+
const comment = line.includes('#') ? ' #' + line.split('#')[1] : '';
85+
lines.push(` ${subKey}: ${value}${comment}`);
86+
logger.debug(`Processing subkey: ${currentKey}.${subKey}, value:`, value);
87+
}
88+
}
89+
else {
90+
lines.push(line);
91+
}
92+
}
93+
94+
configContent = lines.join('\n');
95+
logger.debug('New content to write:', configContent);
96+
97+
try {
98+
const dir = path.dirname(this.configDir);
99+
if (!fs.existsSync(dir)) {
100+
fs.mkdirSync(dir, { recursive: true });
101+
}
102+
103+
fs.writeFileSync(this.configDir, configContent, 'utf8');
104+
logger.info('Config has been saved to file');
105+
}
106+
catch (error) {
107+
logger.error('Failed to write config:', error);
108+
}
109+
110+
this.config = config;
111+
}
112+
53113
checkConfigVersion() {
54114
if (this.default_config.ver > (this.config?.ver ?? 0)) {
55115
logger.warn(`Updating config from version ${this.config?.ver ?? 0} to ${this.default_config.ver}`);
56116

57-
const existingValues = { ...this.config };
58-
59117
let configContent = fs.readFileSync(this.defaultDir, 'utf8');
60118
const lines = configContent.split('\n');
61119
const newLines = [];
62120
let currentKey = '';
63121

122+
const newConfig = JSON.parse(JSON.stringify(this.default_config));
123+
for (const key in this.config) {
124+
if (this.config[key] !== null && this.config[key] !== undefined) {
125+
if (typeof this.config[key] === 'object' && !Array.isArray(this.config[key])) {
126+
newConfig[key] = {
127+
...newConfig[key],
128+
...this.config[key],
129+
};
130+
}
131+
else {
132+
newConfig[key] = this.config[key];
133+
}
134+
}
135+
}
136+
137+
newConfig.ver = this.default_config.ver;
138+
64139
for (const line of lines) {
65140
const keyMatch = line.match(/^(\w+):|^([\w-]+):/);
66-
const indentedKeyMatch = line.match(/^\s+(\w+):/);
141+
const indentedKeyMatch = line.match(/^\s+(\w+):|^\s+([\w-]+):/);
67142

68143
if (keyMatch) {
69144
currentKey = keyMatch[1] || keyMatch[2];
145+
const value = newConfig[currentKey];
70146

71-
if (currentKey === 'ver') {
72-
newLines.push(`ver: ${this.default_config.ver}`);
73-
}
74-
else if (existingValues[currentKey] !== undefined) {
75-
const value = existingValues[currentKey];
76-
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
77-
newLines.push(`${currentKey}:`);
78-
}
79-
else {
80-
newLines.push(`${currentKey}: ${value}`);
81-
}
147+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
148+
newLines.push(`${currentKey}:`);
82149
}
83150
else {
84-
newLines.push(line);
151+
newLines.push(`${currentKey}: ${value}`);
85152
}
86153
}
87154
else if (indentedKeyMatch && currentKey) {
88-
const subKey = indentedKeyMatch[1];
89-
if (existingValues[currentKey]?.[subKey] !== undefined) {
90-
newLines.push(` ${subKey}: ${existingValues[currentKey][subKey]}`);
91-
}
92-
else {
93-
newLines.push(line);
155+
const subKey = indentedKeyMatch[1] || indentedKeyMatch[2];
156+
if (
157+
newConfig[currentKey]
158+
&& typeof newConfig[currentKey][subKey] !== 'undefined'
159+
) {
160+
const value = newConfig[currentKey][subKey];
161+
const comment = line.includes('#') ? ' #' + line.split('#')[1] : '';
162+
newLines.push(` ${subKey}: ${value}${comment}`);
94163
}
95164
}
96165
else {
@@ -107,15 +176,18 @@ class config {
107176
fs.writeFileSync(this.configDir, configContent);
108177
logger.info('Config file updated successfully');
109178

110-
this.config = yaml.load(configContent);
179+
this.config = newConfig;
111180
}
112181
}
113182

114-
getConfig() {
183+
getConfig(refresh = false) {
184+
if (refresh) {
185+
this.readConfigYaml();
186+
}
115187
return this.config;
116188
}
117189
}
118190

119-
new config();
191+
new Config();
120192

121-
module.exports = config;
193+
module.exports = Config;

0 commit comments

Comments
 (0)