- static handleFileException(logPrefix: string, fileType: string, filePath: string, error: NodeJS.ErrnoException, consoleOut = false): void {
- const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : '';
+ static watchJsonFile<T extends JsonType>(
+ logPrefix: string,
+ fileType: FileType,
+ file: string,
+ attribute?: T,
+ listener: fs.WatchListener<string> = (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);
+ } catch (error) {
+ FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, {
+ throwError: false,
+ });
+ }
+ }
+ }
+ ) {
+ if (file) {
+ try {
+ 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`);
+ }
+ }
+
+ static handleFileException(
+ logPrefix: string,
+ fileType: FileType,
+ file: string,
+ error: NodeJS.ErrnoException,
+ params: HandleErrorParams<EmptyObject> = { throwError: true, consoleOut: false }
+ ): void {
+ const prefix = !Utils.isEmptyString(logPrefix) ? logPrefix + ' ' : '';