Cleanup fs operations encoding
[e-mobility-charging-stations-simulator.git] / src / performance / storage / JSONFileStorage.ts
index cc7920a0a4aadfe40ffb6710dddf0e012f1ea9ed..c91a14d5756c90f8b69137d391d64348c6c78a8d 100644 (file)
@@ -16,13 +16,13 @@ export class JSONFileStorage extends Storage {
 
   public storePerformanceStatistics(performanceStatistics: Statistics): void {
     this.checkPerformanceRecordsFile();
-    fs.readFile(this.dbName, 'utf-8', (error, data) => {
+    fs.readFile(this.dbName, 'utf8', (error, data) => {
       if (error) {
         FileUtils.handleFileException(this.logPrefix, Constants.PERFORMANCE_RECORDS_FILETYPE, this.dbName, error);
       } else {
         const performanceRecords: Statistics[] = data ? JSON.parse(data) as Statistics[] : [];
         performanceRecords.push(performanceStatistics);
-        fs.writeFile(this.dbName, JSON.stringify(performanceRecords, null, 2), 'utf-8', (err) => {
+        fs.writeFile(this.dbName, JSON.stringify(performanceRecords, null, 2), 'utf8', (err) => {
           if (err) {
             FileUtils.handleFileException(this.logPrefix, Constants.PERFORMANCE_RECORDS_FILETYPE, this.dbName, err);
           }
@@ -33,7 +33,9 @@ export class JSONFileStorage extends Storage {
 
   public open(): void {
     try {
-      this.fd = fs.openSync(this.dbName, 'a+');
+      if (!this?.fd) {
+        this.fd = fs.openSync(this.dbName, 'a+');
+      }
     } catch (error) {
       FileUtils.handleFileException(this.logPrefix, Constants.PERFORMANCE_RECORDS_FILETYPE, this.dbName, error);
     }
@@ -41,7 +43,7 @@ export class JSONFileStorage extends Storage {
 
   public close(): void {
     try {
-      if (this.fd) {
+      if (this?.fd) {
         fs.closeSync(this.fd);
         this.fd = null;
       }
@@ -51,7 +53,7 @@ export class JSONFileStorage extends Storage {
   }
 
   private checkPerformanceRecordsFile(): void {
-    if (!this.fd) {
+    if (!this?.fd) {
       throw new Error(`${this.logPrefix} Performance records '${this.dbName}' file descriptor not found`);
     }
   }