Skip to content

Commit

Permalink
fix: config
Browse files Browse the repository at this point in the history
whes1015 committed Dec 1, 2024
1 parent dbdf816 commit b83e3db
Showing 1 changed file with 104 additions and 32 deletions.
136 changes: 104 additions & 32 deletions src/js/core/config.js
Original file line number Diff line number Diff line change
@@ -4,12 +4,12 @@ const yaml = require('js-yaml');
const { app } = require('@electron/remote');
const path = require('path');

class config {
class Config {
static instance = null;

constructor() {
if (config.instance) {
return config.instance;
if (Config.instance) {
return Config.instance;
}

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

config.instance = this;
Config.instance = this;
}

static getInstance() {
if (!config.instance) {
new config();
if (!Config.instance) {
new Config();
}

return config.instance;
return Config.instance;
}

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

writeConfig(config = this.config) {
logger.debug('Writing config:', JSON.stringify(config, null, 2));

const lines = [];
let configContent = fs.readFileSync(this.defaultDir, 'utf8');
const templateLines = configContent.split('\n');
let currentKey = '';

for (const line of templateLines) {
const keyMatch = line.match(/^(\w+):|^([\w-]+):/);
const indentedKeyMatch = line.match(/^\s+(\w+):|^\s+([\w-]+):/);

if (keyMatch) {
currentKey = keyMatch[1] || keyMatch[2];
const value = config[currentKey];
logger.debug(`Processing key: ${currentKey}, value:`, value);

if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
lines.push(`${currentKey}:`);
}
else {
lines.push(`${currentKey}: ${value}`);
}
}
else if (indentedKeyMatch && currentKey) {
const subKey = indentedKeyMatch[1] || indentedKeyMatch[2];
if (
config[currentKey]
&& typeof config[currentKey][subKey] !== 'undefined'
) {
const value = config[currentKey][subKey];
const comment = line.includes('#') ? ' #' + line.split('#')[1] : '';
lines.push(` ${subKey}: ${value}${comment}`);
logger.debug(`Processing subkey: ${currentKey}.${subKey}, value:`, value);
}
}
else {
lines.push(line);
}
}

configContent = lines.join('\n');
logger.debug('New content to write:', configContent);

try {
const dir = path.dirname(this.configDir);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}

fs.writeFileSync(this.configDir, configContent, 'utf8');
logger.info('Config has been saved to file');
}
catch (error) {
logger.error('Failed to write config:', error);
}

this.config = config;
}

checkConfigVersion() {
if (this.default_config.ver > (this.config?.ver ?? 0)) {
logger.warn(`Updating config from version ${this.config?.ver ?? 0} to ${this.default_config.ver}`);

const existingValues = { ...this.config };

let configContent = fs.readFileSync(this.defaultDir, 'utf8');
const lines = configContent.split('\n');
const newLines = [];
let currentKey = '';

const newConfig = JSON.parse(JSON.stringify(this.default_config));
for (const key in this.config) {
if (this.config[key] !== null && this.config[key] !== undefined) {
if (typeof this.config[key] === 'object' && !Array.isArray(this.config[key])) {
newConfig[key] = {
...newConfig[key],
...this.config[key],
};
}
else {
newConfig[key] = this.config[key];
}
}
}

newConfig.ver = this.default_config.ver;

for (const line of lines) {
const keyMatch = line.match(/^(\w+):|^([\w-]+):/);
const indentedKeyMatch = line.match(/^\s+(\w+):/);
const indentedKeyMatch = line.match(/^\s+(\w+):|^\s+([\w-]+):/);

if (keyMatch) {
currentKey = keyMatch[1] || keyMatch[2];
const value = newConfig[currentKey];

if (currentKey === 'ver') {
newLines.push(`ver: ${this.default_config.ver}`);
}
else if (existingValues[currentKey] !== undefined) {
const value = existingValues[currentKey];
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
newLines.push(`${currentKey}:`);
}
else {
newLines.push(`${currentKey}: ${value}`);
}
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
newLines.push(`${currentKey}:`);
}
else {
newLines.push(line);
newLines.push(`${currentKey}: ${value}`);
}
}
else if (indentedKeyMatch && currentKey) {
const subKey = indentedKeyMatch[1];
if (existingValues[currentKey]?.[subKey] !== undefined) {
newLines.push(` ${subKey}: ${existingValues[currentKey][subKey]}`);
}
else {
newLines.push(line);
const subKey = indentedKeyMatch[1] || indentedKeyMatch[2];
if (
newConfig[currentKey]
&& typeof newConfig[currentKey][subKey] !== 'undefined'
) {
const value = newConfig[currentKey][subKey];
const comment = line.includes('#') ? ' #' + line.split('#')[1] : '';
newLines.push(` ${subKey}: ${value}${comment}`);
}
}
else {
@@ -107,15 +176,18 @@ class config {
fs.writeFileSync(this.configDir, configContent);
logger.info('Config file updated successfully');

this.config = yaml.load(configContent);
this.config = newConfig;
}
}

getConfig() {
getConfig(refresh = false) {
if (refresh) {
this.readConfigYaml();
}
return this.config;
}
}

new config();
new Config();

module.exports = config;
module.exports = Config;

0 comments on commit b83e3db

Please sign in to comment.