X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FFileUtils.ts;h=8de719a152d35e9da5c16b434a3989ad3e95f782;hb=8eb3b688c4b6fb3e946f38d474a5125caf1d056a;hp=c0891e646a1da0242b31da66375a0251f733cb0f;hpb=9fbc92f7bbead3a303a2a5718d65b0ffd545e24d;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index c0891e64..8de719a1 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -1,21 +1,97 @@ +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 handleFileException(logPrefix: string, fileType: string, filePath: string, error: NodeJS.ErrnoException, consoleOut = false): void { - const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : ''; + private constructor() { + // This is intentional + } + + public static watchJsonFile( + logPrefix: string, + fileType: FileType, + file: string, + refreshedVariable?: T, + listener: fs.WatchListener = (event, filename) => { + if (filename && event === 'change') { + try { + logger.debug(logPrefix + ' ' + fileType + ' file ' + file + ' have changed, reload'); + refreshedVariable && (refreshedVariable = JSON.parse(fs.readFileSync(file, 'utf8')) as T); + } catch (error) { + FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, { + throwError: false, + }); + } + } + } + ): fs.FSWatcher { + if (file) { + try { + return fs.watch(file, listener); + } catch (error) { + FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, { + throwError: false, + }); + } + } else { + logger.info(`${logPrefix} No ${fileType} file to watch given. Not monitoring its changes`); + } + } + + public static handleFileException( + logPrefix: string, + fileType: FileType, + file: string, + error: NodeJS.ErrnoException, + params: HandleErrorParams = { throwError: true, consoleOut: false } + ): void { + const prefix = !Utils.isEmptyString(logPrefix) ? logPrefix + ' ' : ''; if (error.code === 'ENOENT') { - if (consoleOut) { - console.warn(prefix + fileType + ' file ' + filePath + ' not found: ', error); + if (params?.consoleOut) { + console.warn( + chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' not found: '), + error + ); + } else { + logger.warn(prefix + fileType + ' file ' + file + ' not found:', error); + } + } else if (error.code === 'EEXIST') { + if (params?.consoleOut) { + console.warn( + chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' already exists: '), + error + ); + } else { + logger.warn(prefix + fileType + ' file ' + file + ' already exists:', error); + } + } else if (error.code === 'EACCES') { + if (params?.consoleOut) { + console.warn( + chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' access denied: '), + error + ); } else { - logger.warn(prefix + fileType + ' file ' + filePath + ' not found: %j', error); + logger.warn(prefix + fileType + ' file ' + file + ' access denied:', error); } } else { - if (consoleOut) { - console.error(prefix + fileType + ' file ' + filePath + ' opening error: ', error); + if (params?.consoleOut) { + console.warn( + chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' error: '), + error + ); } else { - logger.error(prefix + fileType + ' file ' + filePath + ' opening error: %j', error); + logger.warn(prefix + fileType + ' file ' + file + ' error:', error); + } + if (params?.throwError) { + throw error; } - throw error; } } }