Browse Source

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

Book Pauk 2 năm trước cách đây
mục cha
commit
294fb35f4d
1 tập tin đã thay đổi với 14 bổ sung5 xóa
  1. 14 5
      server/config/index.js

+ 14 - 5
server/config/index.js

@@ -85,13 +85,22 @@ class ConfigManager {
     async load() {
         if (!this.inited)
             throw new Error('not inited');
-        if (!await fs.pathExists(this.userConfigFile)) {
+
+        if (await fs.pathExists(this.userConfigFile)) {
+            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();
-            return;
         }
-
-        const data = await fs.readFile(this.userConfigFile, 'utf8');
-        this.config = JSON.parse(data);
     }
 
     async save() {