Add error handling to JSON schemas file reading
[e-mobility-charging-stations-simulator.git] / src / utils / Configuration.ts
index a67c70c25908c196019f7a344ac7cf34467a6c2f..263fa04876d92d90080aae3946f4a36864a9e7d6 100644 (file)
@@ -1,6 +1,6 @@
-import fs from 'fs';
-import path from 'path';
-import { fileURLToPath } from 'url';
+import fs from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
 
 import chalk from 'chalk';
 import merge from 'just-merge';
@@ -72,7 +72,7 @@ export default class Configuration {
       uiServerConfiguration = merge(uiServerConfiguration, Configuration.getConfig()?.uiServer);
     }
     if (Configuration.isCFEnvironment() === true) {
-      delete uiServerConfiguration.options.host;
+      delete uiServerConfiguration.options?.host;
       uiServerConfiguration.options.port = parseInt(process.env.PORT);
     }
     return uiServerConfiguration;
@@ -336,13 +336,13 @@ export default class Configuration {
     ) {
       console.error(
         chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration key '${key}' usage in section '${sectionName}'${
-          logMsgToAppend && `. ${logMsgToAppend}`
+          logMsgToAppend.trim().length !== 0 && `. ${logMsgToAppend}`
         }}`
       );
     } else if (!Configuration.isUndefined(Configuration.getConfig()[key])) {
       console.error(
         chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration key '${key}' usage${
-          logMsgToAppend && `. ${logMsgToAppend}`
+          logMsgToAppend.trim().length !== 0 && `. ${logMsgToAppend}`
         }}`
       );
     }
@@ -373,11 +373,11 @@ export default class Configuration {
   private static getConfigurationFileWatcher(): fs.FSWatcher | undefined {
     try {
       return fs.watch(Configuration.configurationFile, (event, filename): void => {
-        if (filename && event === 'change') {
+        if (filename.trim().length !== 0 && event === 'change') {
           // Nullify to force configuration file reading
           Configuration.configuration = null;
           if (!Configuration.isUndefined(Configuration.configurationChangeCallback)) {
-            Configuration.configurationChangeCallback().catch(error => {
+            Configuration.configurationChangeCallback().catch((error) => {
               throw typeof error === 'string' ? new Error(error) : error;
             });
           }