Merge pull request #783 from SAP/dependabot/npm_and_yarn/mikro-orm/sqlite-5.8.8
[e-mobility-charging-stations-simulator.git] / src / utils / Configuration.ts
index 190a31a350f1a8c405bb47a96165c4643c09f9d8..d994422b07bf91e176fd7d36bc680318fadabc4c 100644 (file)
@@ -1,12 +1,20 @@
 import { type FSWatcher, readFileSync, watch } from 'node:fs';
 import { dirname, join, resolve } from 'node:path';
+import { env } from 'node:process';
 import { fileURLToPath } from 'node:url';
 
 import chalk from 'chalk';
 import merge from 'just-merge';
 
 import { Constants } from './Constants';
-import { hasOwnProp, isCFEnvironment, isNotEmptyString, isUndefined, once } from './Utils';
+import {
+  hasOwnProp,
+  isCFEnvironment,
+  isNotEmptyString,
+  isUndefined,
+  logPrefix,
+  once,
+} from './Utils';
 import {
   ApplicationProtocol,
   type ConfigurationData,
@@ -34,7 +42,14 @@ type ConfigurationSectionType =
   | WorkerConfiguration
   | UIServerConfiguration;
 
+// Avoid ESM race condition at class initialization
+const configurationLogPrefix = (): string => {
+  return logPrefix(' Simulator configuration |');
+};
+
 export class Configuration {
+  public static configurationChangeCallback: () => Promise<void>;
+
   private static configurationFile = join(
     dirname(fileURLToPath(import.meta.url)),
     'assets',
@@ -53,16 +68,10 @@ export class Configuration {
     [ConfigurationSection.uiServer, Configuration.buildUIServerSection()],
   ]);
 
-  private static configurationChangeCallback?: () => Promise<void>;
-
   private constructor() {
     // This is intentional
   }
 
-  public static setConfigurationChangeCallback(cb: () => Promise<void>): void {
-    Configuration.configurationChangeCallback = cb;
-  }
-
   public static getConfigurationSection<T extends ConfigurationSectionType>(
     sectionName: ConfigurationSection,
   ): T {
@@ -101,7 +110,7 @@ export class Configuration {
   }
 
   public static workerPoolInUse(): boolean {
-    return [WorkerProcessType.dynamicPool, WorkerProcessType.staticPool].includes(
+    return [WorkerProcessType.dynamicPool, WorkerProcessType.fixedPool].includes(
       Configuration.getConfigurationSection<WorkerConfiguration>(ConfigurationSection.worker)
         .processType!,
     );
@@ -164,7 +173,7 @@ export class Configuration {
     }
     if (isCFEnvironment() === true) {
       delete uiServerConfiguration.options?.host;
-      uiServerConfiguration.options!.port = parseInt(process.env.PORT!);
+      uiServerConfiguration.options!.port = parseInt(env.PORT!);
     }
     return uiServerConfiguration;
   }
@@ -287,10 +296,6 @@ export class Configuration {
     return workerConfiguration;
   }
 
-  private static logPrefix = (): string => {
-    return `${new Date().toLocaleString()} Simulator configuration |`;
-  };
-
   private static checkDeprecatedConfigurationKeys() {
     // connection timeout
     Configuration.warnDeprecatedConfigurationKey(
@@ -326,7 +331,7 @@ export class Configuration {
       (stationTemplateUrl: StationTemplateUrl) => {
         if (!isUndefined(stationTemplateUrl?.['numberOfStation' as keyof StationTemplateUrl])) {
           console.error(
-            `${chalk.green(Configuration.logPrefix())} ${chalk.red(
+            `${chalk.green(configurationLogPrefix())} ${chalk.red(
               `Deprecated configuration key 'numberOfStation' usage for template file '${stationTemplateUrl.file}' in 'stationTemplateUrls'. Use 'numberOfStations' instead`,
             )}`,
           );
@@ -382,17 +387,17 @@ export class Configuration {
       `Use '${ConfigurationSection.worker}' section to define the worker pool minimum size instead`,
     );
     Configuration.warnDeprecatedConfigurationKey(
-      'workerPoolSize;',
+      'workerPoolSize',
       undefined,
       `Use '${ConfigurationSection.worker}' section to define the worker pool maximum size instead`,
     );
     Configuration.warnDeprecatedConfigurationKey(
-      'workerPoolMaxSize;',
+      'workerPoolMaxSize',
       undefined,
       `Use '${ConfigurationSection.worker}' section to define the worker pool maximum size instead`,
     );
     Configuration.warnDeprecatedConfigurationKey(
-      'workerPoolStrategy;',
+      'workerPoolStrategy',
       undefined,
       `Use '${ConfigurationSection.worker}' section to define the worker pool strategy instead`,
     );
@@ -401,6 +406,16 @@ export class Configuration {
       ConfigurationSection.worker,
       'Not publicly exposed to end users',
     );
+    if (
+      Configuration.getConfigurationData()?.worker?.processType ===
+      ('staticPool' as WorkerProcessType)
+    ) {
+      console.error(
+        `${chalk.green(configurationLogPrefix())} ${chalk.red(
+          `Deprecated configuration 'staticPool' value usage in worker section 'processType' field. Use '${WorkerProcessType.fixedPool}' value instead`,
+        )}`,
+      );
+    }
     // log section
     Configuration.warnDeprecatedConfigurationKey(
       'logEnabled',
@@ -461,7 +476,7 @@ export class Configuration {
     // uiServer section
     if (hasOwnProp(Configuration.getConfigurationData(), 'uiWebSocketServer')) {
       console.error(
-        `${chalk.green(Configuration.logPrefix())} ${chalk.red(
+        `${chalk.green(configurationLogPrefix())} ${chalk.red(
           `Deprecated configuration section 'uiWebSocketServer' usage. Use '${ConfigurationSection.uiServer}' instead`,
         )}`,
       );
@@ -488,7 +503,7 @@ export class Configuration {
       )
     ) {
       console.error(
-        `${chalk.green(Configuration.logPrefix())} ${chalk.red(
+        `${chalk.green(configurationLogPrefix())} ${chalk.red(
           `Deprecated configuration key '${key}' usage in section '${sectionName}'${
             logMsgToAppend.trim().length > 0 ? `. ${logMsgToAppend}` : ''
           }`,
@@ -498,7 +513,7 @@ export class Configuration {
       !isUndefined(Configuration.getConfigurationData()?.[key as keyof ConfigurationData])
     ) {
       console.error(
-        `${chalk.green(Configuration.logPrefix())} ${chalk.red(
+        `${chalk.green(configurationLogPrefix())} ${chalk.red(
           `Deprecated configuration key '${key}' usage${
             logMsgToAppend.trim().length > 0 ? `. ${logMsgToAppend}` : ''
           }`,
@@ -521,7 +536,7 @@ export class Configuration {
           Configuration.configurationFile,
           FileType.Configuration,
           error as NodeJS.ErrnoException,
-          Configuration.logPrefix(),
+          configurationLogPrefix(),
         );
       }
     }
@@ -535,7 +550,7 @@ export class Configuration {
           delete Configuration.configurationData;
           Configuration.configurationSectionCache.clear();
           if (!isUndefined(Configuration.configurationChangeCallback)) {
-            Configuration.configurationChangeCallback!().catch((error) => {
+            Configuration.configurationChangeCallback().catch((error) => {
               throw typeof error === 'string' ? new Error(error) : error;
             });
           }
@@ -546,7 +561,7 @@ export class Configuration {
         Configuration.configurationFile,
         FileType.Configuration,
         error as NodeJS.ErrnoException,
-        Configuration.logPrefix(),
+        configurationLogPrefix(),
       );
     }
   }
@@ -555,27 +570,27 @@ export class Configuration {
     file: string,
     fileType: FileType,
     error: NodeJS.ErrnoException,
-    logPrefix: string,
+    logPfx: string,
   ): void {
-    const prefix = isNotEmptyString(logPrefix) ? `${logPrefix} ` : '';
+    const prefix = isNotEmptyString(logPfx) ? `${logPfx} ` : '';
     let logMsg: string;
     switch (error.code) {
       case 'ENOENT':
-        logMsg = `${fileType} file ${file} not found:`;
+        logMsg = `${fileType} file ${file} not found: `;
         break;
       case 'EEXIST':
-        logMsg = `${fileType} file ${file} already exists:`;
+        logMsg = `${fileType} file ${file} already exists: `;
         break;
       case 'EACCES':
-        logMsg = `${fileType} file ${file} access denied:`;
+        logMsg = `${fileType} file ${file} access denied: `;
         break;
       case 'EPERM':
-        logMsg = `${fileType} file ${file} permission denied:`;
+        logMsg = `${fileType} file ${file} permission denied: `;
         break;
       default:
-        logMsg = `${fileType} file ${file} error:`;
+        logMsg = `${fileType} file ${file} error: `;
     }
-    console.error(`${chalk.green(prefix)}${chalk.red(`${logMsg} `)}`, error);
+    console.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error);
     throw error;
   }