X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fperformance%2Fstorage%2FJsonFileStorage.ts;h=c4a9f7483e30060047607bdbad5a97e15fb7c91c;hb=3e8d029bbd0a072ae80c45e9a50770de3218cbc1;hp=2a712a6008f61722ef21dddaac5acd2e3ba14d53;hpb=7b5dbe910fc30484324da90ee209ab7002bc5071;p=e-mobility-charging-stations-simulator.git diff --git a/src/performance/storage/JsonFileStorage.ts b/src/performance/storage/JsonFileStorage.ts index 2a712a60..c4a9f748 100644 --- a/src/performance/storage/JsonFileStorage.ts +++ b/src/performance/storage/JsonFileStorage.ts @@ -1,12 +1,19 @@ // Copyright Jerome Benoit. 2021-2023. All Rights Reserved. -import fs from 'node:fs'; -import path from 'node:path'; +import { closeSync, existsSync, mkdirSync, openSync, readFileSync, writeFileSync } from 'node:fs'; +import { dirname } from 'node:path'; import { Storage } from './Storage'; import { BaseError } from '../../exception'; import { FileType, type Statistics } from '../../types'; -import { AsyncLock, AsyncLockType, Constants, ErrorUtils, Utils } from '../../utils'; +import { + AsyncLock, + AsyncLockType, + Constants, + JSONStringifyWithMapSupport, + handleFileException, + isNullOrUndefined, +} from '../../utils'; export class JsonFileStorage extends Storage { private fd: number | null = null; @@ -20,19 +27,15 @@ export class JsonFileStorage extends Storage { this.checkPerformanceRecordsFile(); AsyncLock.acquire(AsyncLockType.performance) .then(() => { - const fileData = fs.readFileSync(this.dbName, 'utf8'); + const fileData = readFileSync(this.dbName, 'utf8'); const performanceRecords: Statistics[] = fileData ? (JSON.parse(fileData) as Statistics[]) : []; performanceRecords.push(performanceStatistics); - fs.writeFileSync( - this.dbName, - Utils.JSONStringifyWithMapSupport(performanceRecords, 2), - 'utf8' - ); + writeFileSync(this.dbName, JSONStringifyWithMapSupport(performanceRecords, 2), 'utf8'); }) .catch((error) => { - ErrorUtils.handleFileException( + handleFileException( this.dbName, FileType.PerformanceRecords, error as NodeJS.ErrnoException, @@ -46,14 +49,14 @@ export class JsonFileStorage extends Storage { public open(): void { try { - if (Utils.isNullOrUndefined(this?.fd)) { - if (!fs.existsSync(path.dirname(this.dbName))) { - fs.mkdirSync(path.dirname(this.dbName), { recursive: true }); + if (isNullOrUndefined(this?.fd)) { + if (!existsSync(dirname(this.dbName))) { + mkdirSync(dirname(this.dbName), { recursive: true }); } - this.fd = fs.openSync(this.dbName, 'a+'); + this.fd = openSync(this.dbName, 'a+'); } } catch (error) { - ErrorUtils.handleFileException( + handleFileException( this.dbName, FileType.PerformanceRecords, error as NodeJS.ErrnoException, @@ -65,11 +68,11 @@ export class JsonFileStorage extends Storage { public close(): void { try { if (this?.fd) { - fs.closeSync(this.fd); + closeSync(this.fd); this.fd = null; } } catch (error) { - ErrorUtils.handleFileException( + handleFileException( this.dbName, FileType.PerformanceRecords, error as NodeJS.ErrnoException,