Add OCPP params file monitoring
[e-mobility-charging-stations-simulator.git] / src / utils / FileUtils.ts
index 0e8efc61e38cf593143a085631adb5f2b8db2efe..cab28941e730f831ed58f3639863ecfa97767bfd 100644 (file)
@@ -1,52 +1,88 @@
+import { JsonType, JsonValue } from '../types/JsonType';
+
 import { EmptyObject } from '../types/EmptyObject';
+import { FileType } from '../types/FileType';
 import { HandleErrorParams } from '../types/Error';
+import Utils from './Utils';
 import chalk from 'chalk';
+import fs from 'fs';
 import logger from './Logger';
 
 export default class FileUtils {
+  static watchJsonFile<T extends JsonType | JsonValue>(
+    logPrefix: string,
+    fileType: FileType,
+    file: string,
+    attribute?: T,
+    listener: fs.WatchListener<string> = (event, filename) => {
+      if (filename && event === 'change') {
+        try {
+          logger.debug(logPrefix + ' ' + fileType + ' file ' + file + ' have changed, reload');
+          attribute = JSON.parse(fs.readFileSync(file, 'utf8')) as T;
+        } catch (error) {
+          FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, {
+            throwError: false,
+          });
+        }
+      }
+    }
+  ) {
+    if (file) {
+      try {
+        fs.watch(file, listener);
+      } catch (error) {
+        FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, {
+          throwError: false,
+        });
+      }
+    } else {
+      logger.info(`${logPrefix} No ${fileType} file to watch given. Not monitoring its changes`);
+    }
+  }
+
   static handleFileException(
     logPrefix: string,
-    fileType: string,
-    filePath: string,
+    fileType: FileType,
+    file: string,
     error: NodeJS.ErrnoException,
     params: HandleErrorParams<EmptyObject> = { throwError: true, consoleOut: false }
   ): void {
-    const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : '';
+    const prefix = !Utils.isEmptyString(logPrefix) ? logPrefix + ' ' : '';
     if (error.code === 'ENOENT') {
       if (params?.consoleOut) {
         console.warn(
-          chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' not found: '),
+          chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' not found: '),
           error
         );
       } else {
-        logger.warn(prefix + fileType + ' file ' + filePath + ' not found: %j', error);
+        logger.warn(prefix + fileType + ' file ' + file + ' not found: %j', error);
       }
     } else if (error.code === 'EEXIST') {
       if (params?.consoleOut) {
         console.warn(
-          chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' already exists: '),
+          chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' already exists: '),
           error
         );
       } else {
-        logger.warn(prefix + fileType + ' file ' + filePath + ' already exists: %j', error);
+        logger.warn(prefix + fileType + ' file ' + file + ' already exists: %j', error);
       }
     } else if (error.code === 'EACCES') {
       if (params?.consoleOut) {
         console.warn(
-          chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' access denied: '),
+          chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' access denied: '),
           error
         );
       } else {
-        logger.warn(prefix + fileType + ' file ' + filePath + ' access denied: %j', error);
+        logger.warn(prefix + fileType + ' file ' + file + ' access denied: %j', error);
       }
     } else {
       if (params?.consoleOut) {
         console.warn(
-          chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' error: '),
+          chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' error: '),
           error
         );
       } else {
-        logger.warn(prefix + fileType + ' file ' + filePath + ' error: %j', error);
+        logger.warn(prefix + fileType + ' file ' + file + ' error: %j', error);
       }
       if (params?.throwError) {
         throw error;