repositories
/
e-mobility-charging-stations-simulator.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
refactor: cleanup getLimitFromChargingProfiles() helper return type
[e-mobility-charging-stations-simulator.git]
/
src
/
performance
/
storage
/
JsonFileStorage.ts
diff --git
a/src/performance/storage/JsonFileStorage.ts
b/src/performance/storage/JsonFileStorage.ts
index 0fd29d0214f4ec6a138281f1e84d61011591ae03..8bbbd42cea06563d91d7e6d360bf38cc06f92563 100644
(file)
--- a/
src/performance/storage/JsonFileStorage.ts
+++ b/
src/performance/storage/JsonFileStorage.ts
@@
-1,12
+1,19
@@
// Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
// 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 { Storage } from './Storage';
import { BaseError } from '../../exception';
import { FileType, type Statistics } from '../../types';
-import { AsyncLock, AsyncLockType, Constants, Utils, handleFileException } from '../../utils';
+import {
+ AsyncLock,
+ AsyncLockType,
+ Constants,
+ JSONStringifyWithMapSupport,
+ handleFileException,
+ isNullOrUndefined,
+} from '../../utils';
export class JsonFileStorage extends Storage {
private fd: number | null = null;
export class JsonFileStorage extends Storage {
private fd: number | null = null;
@@
-20,23
+27,19
@@
export class JsonFileStorage extends Storage {
this.checkPerformanceRecordsFile();
AsyncLock.acquire(AsyncLockType.performance)
.then(() => {
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);
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) => {
handleFileException(
this.dbName,
FileType.PerformanceRecords,
error as NodeJS.ErrnoException,
})
.catch((error) => {
handleFileException(
this.dbName,
FileType.PerformanceRecords,
error as NodeJS.ErrnoException,
- this.logPrefix
+ this.logPrefix
,
);
})
.finally(() => {
);
})
.finally(() => {
@@
-46,18
+49,18
@@
export class JsonFileStorage extends Storage {
public open(): void {
try {
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) {
handleFileException(
this.dbName,
FileType.PerformanceRecords,
error as NodeJS.ErrnoException,
}
} catch (error) {
handleFileException(
this.dbName,
FileType.PerformanceRecords,
error as NodeJS.ErrnoException,
- this.logPrefix
+ this.logPrefix
,
);
}
}
);
}
}
@@
-65,7
+68,7
@@
export class JsonFileStorage extends Storage {
public close(): void {
try {
if (this?.fd) {
public close(): void {
try {
if (this?.fd) {
-
fs.
closeSync(this.fd);
+ closeSync(this.fd);
this.fd = null;
}
} catch (error) {
this.fd = null;
}
} catch (error) {
@@
-73,7
+76,7
@@
export class JsonFileStorage extends Storage {
this.dbName,
FileType.PerformanceRecords,
error as NodeJS.ErrnoException,
this.dbName,
FileType.PerformanceRecords,
error as NodeJS.ErrnoException,
- this.logPrefix
+ this.logPrefix
,
);
}
}
);
}
}
@@
-81,7
+84,7
@@
export class JsonFileStorage extends Storage {
private checkPerformanceRecordsFile(): void {
if (!this?.fd) {
throw new BaseError(
private checkPerformanceRecordsFile(): void {
if (!this?.fd) {
throw new BaseError(
- `${this.logPrefix} Performance records '${this.dbName}' file descriptor not found`
+ `${this.logPrefix} Performance records '${this.dbName}' file descriptor not found`
,
);
}
}
);
}
}