X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FFileUtils.ts;h=8de719a152d35e9da5c16b434a3989ad3e95f782;hb=adbddcb44d65e7268f661deacc4291d6cf6f6663;hp=b057d7f7e844b4684f823903bc22a60f98e200dd;hpb=3e0905a14af7b7e0a96e859d6c4c615044845d54;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index b057d7f7..8de719a1 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -1,23 +1,29 @@ -import { EmptyObject } from '../types/EmptyObject'; -import { FileType } from '../types/FileType'; -import { HandleErrorParams } from '../types/Error'; -import { JsonType } from '../types/JsonType'; -import Utils from './Utils'; -import chalk from 'chalk'; import fs from 'fs'; + +import chalk from 'chalk'; + +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 && (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, @@ -25,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, @@ -39,7 +45,7 @@ export default class FileUtils { } } - static handleFileException( + public static handleFileException( logPrefix: string, fileType: FileType, file: string, @@ -54,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) { @@ -63,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) { @@ -72,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) { @@ -81,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;