X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FFileUtils.ts;h=bc8dd07c24725d990ae282056fc5ab8d4243b942;hb=4ccf551d961ef001acef6fafe1165ad9b6dac4e3;hp=51871884ccc38e7c7b6d09c7d6cd37c1a6ad3192;hpb=fa5995d65e5084241af14d0ab7453fbb2fc9d8a6;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index 51871884..bc8dd07c 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -1,37 +1,38 @@ -import fs from 'node:fs'; +import { type FSWatcher, type WatchListener, readFileSync, watch } from 'node:fs' -import { handleFileException } from './ErrorUtils'; -import { logger } from './Logger'; -import { Utils } from './Utils'; -import type { FileType, JsonType } from '../types'; +import { handleFileException } from './ErrorUtils.js' +import { logger } from './Logger.js' +import { isNotEmptyString } from './Utils.js' +import type { FileType, JsonType } from '../types/index.js' export const watchJsonFile = ( file: string, fileType: FileType, logPrefix: string, refreshedVariable?: T, - listener: fs.WatchListener = (event, filename) => { - if (Utils.isNotEmptyString(filename) && event === 'change') { + listener: WatchListener = (event, filename) => { + if (isNotEmptyString(filename) && event === 'change') { try { - logger.debug(`${logPrefix} ${fileType} file ${file} have changed, reload`); - refreshedVariable && (refreshedVariable = JSON.parse(fs.readFileSync(file, 'utf8')) as T); + logger.debug(`${logPrefix} ${fileType} file ${file} have changed, reload`) + refreshedVariable != null && + (refreshedVariable = JSON.parse(readFileSync(file, 'utf8')) as T) } catch (error) { handleFileException(file, fileType, error as NodeJS.ErrnoException, logPrefix, { - throwError: false, - }); + throwError: false + }) } } } -): fs.FSWatcher | undefined => { - if (Utils.isNotEmptyString(file)) { +): FSWatcher | undefined => { + if (isNotEmptyString(file)) { try { - return fs.watch(file, listener); + return watch(file, listener) } catch (error) { handleFileException(file, fileType, error as NodeJS.ErrnoException, logPrefix, { - throwError: false, - }); + throwError: false + }) } } else { - logger.info(`${logPrefix} No ${fileType} file to watch given. Not monitoring its changes`); + logger.info(`${logPrefix} No ${fileType} file to watch given. Not monitoring its changes`) } -}; +}