X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FFileUtils.ts;h=8de719a152d35e9da5c16b434a3989ad3e95f782;hb=adbddcb44d65e7268f661deacc4291d6cf6f6663;hp=cab28941e730f831ed58f3639863ecfa97767bfd;hpb=a95873d8d308a20a7151346ac70d9a551f1a06f5;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index cab28941..8de719a1 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -1,24 +1,29 @@ -import { JsonType, JsonValue } 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 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 Utils from './Utils'; export default class FileUtils { - static watchJsonFile( + private constructor() { + // This is intentional + } + + public static watchJsonFile( logPrefix: string, fileType: FileType, file: string, - attribute?: T, + refreshedVariable?: T, listener: fs.WatchListener = (event, filename) => { if (filename && event === 'change') { try { logger.debug(logPrefix + ' ' + fileType + ' file ' + file + ' have changed, reload'); - attribute = JSON.parse(fs.readFileSync(file, 'utf8')) as T; + refreshedVariable && (refreshedVariable = JSON.parse(fs.readFileSync(file, 'utf8')) as T); } 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 { - fs.watch(file, listener); + return fs.watch(file, listener); } 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, @@ -55,7 +60,7 @@ export default class FileUtils { 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) { @@ -64,7 +69,7 @@ export default class FileUtils { 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) { @@ -73,7 +78,7 @@ export default class FileUtils { 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) { @@ -82,7 +87,7 @@ export default class FileUtils { error ); } else { - logger.warn(prefix + fileType + ' file ' + file + ' error: %j', error); + logger.warn(prefix + fileType + ' file ' + file + ' error:', error); } if (params?.throwError) { throw error;