From ea9d1a74b8e7f3b2fbf7d17f492f16c6eb762b73 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me>
Date: Mon, 3 Jul 2023 23:50:52 +0200
Subject: [PATCH] LocalConfig => Bug fix: File can be empty when the soft stop
 running suddenly

---
 NodeApp/src/config/LocalConfig.ts | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/NodeApp/src/config/LocalConfig.ts b/NodeApp/src/config/LocalConfig.ts
index 4be9e9a..b150d5e 100644
--- a/NodeApp/src/config/LocalConfig.ts
+++ b/NodeApp/src/config/LocalConfig.ts
@@ -32,10 +32,12 @@ class LocalConfig {
             fs.writeFileSync(this.configPath, JSON.stringify({}));
         }
 
-        this._config = JSON.parse(fs.readFileSync(this.configPath).toString());
+        try {
+            this._config = JSON.parse(fs.readFileSync(this.configPath).toString());
 
-        SessionManager.token = this._config.apiToken;
-        GitlabManager.token = this._config.gitlabPersonalToken;
+            SessionManager.token = this._config.apiToken;
+            GitlabManager.token = this._config.gitlabPersonalToken;
+        } catch ( error ) { }
     }
 
     updateConfig(key: LocalConfigKeys, value: any) {
@@ -45,11 +47,11 @@ class LocalConfig {
 
         (this._config as any)[key] = value;
 
-        fs.writeFile(this.configPath, JSON.stringify(this._config, null, 4), (err) => {
-            if ( err ) {
-                logger.error(err);
-            }
-        });
+        try {
+            fs.writeFileSync(this.configPath, JSON.stringify(this._config, null, 4));
+        } catch ( error ) {
+            logger.error(error);
+        }
     }
 }
 
-- 
GitLab