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
Fix broadcast channel payload cleanup for ATG
[e-mobility-charging-stations-simulator.git]
/
src
/
utils
/
FileUtils.ts
diff --git
a/src/utils/FileUtils.ts
b/src/utils/FileUtils.ts
index c61ff8f4f8d245a680b8b429ec5db6d4668fcb58..8de719a152d35e9da5c16b434a3989ad3e95f782 100644
(file)
--- a/
src/utils/FileUtils.ts
+++ b/
src/utils/FileUtils.ts
@@
-1,24
+1,29
@@
-import
{ JsonObject, JsonType } from '../types/JsonType
';
+import
fs from 'fs
';
-import { EmptyObject } from '../types/EmptyObject';
-import { FileType } from '../types/FileType';
-import { HandleErrorParams } from '../types/Error';
-import Utils from './Utils';
import chalk from 'chalk';
import chalk from 'chalk';
-import fs from 'fs';
+
+import type { EmptyObject } from '../types/EmptyObject';
+import type { HandleErrorParams } from '../types/Error';
+import type { FileType } from '../types/FileType';
+import type { JsonType } from '../types/JsonType';
import logger from './Logger';
import logger from './Logger';
+import Utils from './Utils';
export default class FileUtils {
export default class FileUtils {
- static watchJsonFile<T extends JsonObject | JsonType>(
+ private constructor() {
+ // This is intentional
+ }
+
+ public static watchJsonFile<T extends JsonType>(
logPrefix: string,
fileType: FileType,
file: string,
logPrefix: string,
fileType: FileType,
file: string,
-
attribut
e?: T,
+
refreshedVariabl
e?: T,
listener: fs.WatchListener<string> = (event, filename) => {
if (filename && event === 'change') {
try {
logger.debug(logPrefix + ' ' + fileType + ' file ' + file + ' have changed, reload');
listener: fs.WatchListener<string> = (event, filename) => {
if (filename && event === 'change') {
try {
logger.debug(logPrefix + ' ' + fileType + ' file ' + file + ' have changed, reload');
-
attribute && (attribut
e = JSON.parse(fs.readFileSync(file, 'utf8')) as T);
+
refreshedVariable && (refreshedVariabl
e = JSON.parse(fs.readFileSync(file, 'utf8')) as T);
} catch (error) {
FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, {
throwError: false,
} catch (error) {
FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, {
throwError: false,
@@
-26,10
+31,10
@@
export default class FileUtils {
}
}
}
}
}
}
- ) {
+ )
: fs.FSWatcher
{
if (file) {
try {
if (file) {
try {
- fs.watch(file, listener);
+
return
fs.watch(file, listener);
} catch (error) {
FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, {
throwError: false,
} catch (error) {
FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, {
throwError: false,
@@
-40,7
+45,7
@@
export default class FileUtils {
}
}
}
}
- static handleFileException(
+
public
static handleFileException(
logPrefix: string,
fileType: FileType,
file: string,
logPrefix: string,
fileType: FileType,
file: string,
@@
-55,7
+60,7
@@
export default class FileUtils {
error
);
} else {
error
);
} else {
- logger.warn(prefix + fileType + ' file ' + file + ' not found:
%j
', error);
+ logger.warn(prefix + fileType + ' file ' + file + ' not found:', error);
}
} else if (error.code === 'EEXIST') {
if (params?.consoleOut) {
}
} else if (error.code === 'EEXIST') {
if (params?.consoleOut) {
@@
-64,7
+69,7
@@
export default class FileUtils {
error
);
} else {
error
);
} else {
- logger.warn(prefix + fileType + ' file ' + file + ' already exists:
%j
', error);
+ logger.warn(prefix + fileType + ' file ' + file + ' already exists:', error);
}
} else if (error.code === 'EACCES') {
if (params?.consoleOut) {
}
} else if (error.code === 'EACCES') {
if (params?.consoleOut) {
@@
-73,7
+78,7
@@
export default class FileUtils {
error
);
} else {
error
);
} else {
- logger.warn(prefix + fileType + ' file ' + file + ' access denied:
%j
', error);
+ logger.warn(prefix + fileType + ' file ' + file + ' access denied:', error);
}
} else {
if (params?.consoleOut) {
}
} else {
if (params?.consoleOut) {
@@
-82,7
+87,7
@@
export default class FileUtils {
error
);
} else {
error
);
} else {
- logger.warn(prefix + fileType + ' file ' + file + ' error:
%j
', error);
+ logger.warn(prefix + fileType + ' file ' + file + ' error:', error);
}
if (params?.throwError) {
throw error;
}
if (params?.throwError) {
throw error;