Улучшение загрузки конфига

This commit is contained in:
Book Pauk
2022-10-21 13:47:10 +07:00
parent 1490fc854a
commit 294fb35f4d

View File

@@ -85,13 +85,22 @@ class ConfigManager {
async load() { async load() {
if (!this.inited) if (!this.inited)
throw new Error('not inited'); throw new Error('not inited');
if (!await fs.pathExists(this.userConfigFile)) {
await this.save();
return;
}
const data = await fs.readFile(this.userConfigFile, 'utf8'); if (await fs.pathExists(this.userConfigFile)) {
this.config = JSON.parse(data); const data = JSON.parse(await fs.readFile(this.userConfigFile, 'utf8'));
const config = _.pick(data, propsToSave);
this.config = config;
//сохраним конфиг, если не все атрибуты присутствуют в файле конфига
for (const prop of propsToSave)
if (!Object.prototype.hasOwnProperty.call(config, prop)) {
await this.save();
break;
}
} else {
await this.save();
}
} }
async save() { async save() {