feat: set supervision url through the UI protocol
[e-mobility-charging-stations-simulator.git] / src / charging-station / AuthorizedTagsCache.ts
index 4f853a971fc537fe35f872fbeaf6f2075a50e830..332b3a983a8b8760ef33668639ec09b3aaf24d9c 100644 (file)
@@ -1,18 +1,16 @@
-import fs from 'fs';
+import fs from 'node:fs';
 
-import { FileType } from '../types/FileType';
-import FileUtils from '../utils/FileUtils';
-import logger from '../utils/Logger';
-import Utils from '../utils/Utils';
+import { FileType } from '../types';
+import { FileUtils, Utils, logger } from '../utils';
 
-export default class AuthorizedTagsCache {
+export class AuthorizedTagsCache {
   private static instance: AuthorizedTagsCache | null = null;
   private readonly tagsCaches: Map<string, string[]>;
-  private readonly FSWatchers: Map<string, fs.FSWatcher>;
+  private readonly FSWatchers: Map<string, fs.FSWatcher | undefined>;
 
   private constructor() {
     this.tagsCaches = new Map<string, string[]>();
-    this.FSWatchers = new Map<string, fs.FSWatcher>();
+    this.FSWatchers = new Map<string, fs.FSWatcher | undefined>();
   }
 
   public static getInstance(): AuthorizedTagsCache {
@@ -22,7 +20,7 @@ export default class AuthorizedTagsCache {
     return AuthorizedTagsCache.instance;
   }
 
-  public getAuthorizedTags(file: string): string[] {
+  public getAuthorizedTags(file: string): string[] | undefined {
     if (this.hasTags(file) === false) {
       this.setTags(file, this.getAuthorizedTagsFromFile(file));
       // Monitor authorization file
@@ -30,12 +28,12 @@ export default class AuthorizedTagsCache {
         this.FSWatchers.set(
           file,
           FileUtils.watchJsonFile(
-            this.logPrefix(file),
-            FileType.Authorization,
             file,
-            null,
+            FileType.Authorization,
+            this.logPrefix(file),
+            undefined,
             (event, filename) => {
-              if (filename && event === 'change') {
+              if (Utils.isNotEmptyString(filename) && event === 'change') {
                 try {
                   logger.debug(
                     `${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload`
@@ -44,10 +42,10 @@ export default class AuthorizedTagsCache {
                   this.deleteFSWatcher(file);
                 } catch (error) {
                   FileUtils.handleFileException(
-                    this.logPrefix(file),
-                    FileType.Authorization,
                     file,
+                    FileType.Authorization,
                     error as NodeJS.ErrnoException,
+                    this.logPrefix(file),
                     {
                       throwError: false,
                     }
@@ -73,7 +71,7 @@ export default class AuthorizedTagsCache {
     return this.tagsCaches.set(file, tags);
   }
 
-  private getTags(file: string): string[] {
+  private getTags(file: string): string[] | undefined {
     return this.tagsCaches.get(file);
   }
 
@@ -82,7 +80,7 @@ export default class AuthorizedTagsCache {
   }
 
   private deleteFSWatcher(file: string): boolean {
-    this.FSWatchers.get(file).close();
+    this.FSWatchers.get(file)?.close();
     return this.FSWatchers.delete(file);
   }
 
@@ -94,10 +92,10 @@ export default class AuthorizedTagsCache {
         authorizedTags = JSON.parse(fs.readFileSync(file, 'utf8')) as string[];
       } catch (error) {
         FileUtils.handleFileException(
-          this.logPrefix(file),
-          FileType.Authorization,
           file,
-          error as NodeJS.ErrnoException
+          FileType.Authorization,
+          error as NodeJS.ErrnoException,
+          this.logPrefix(file)
         );
       }
     } else {
@@ -106,7 +104,7 @@ export default class AuthorizedTagsCache {
     return authorizedTags;
   }
 
-  private logPrefix(file: string): string {
+  private logPrefix = (file: string): string => {
     return Utils.logPrefix(` Authorized tags cache for authorization file '${file}' |`);
-  }
+  };
 }