-export default class FileUtils {
- static handleFileException(logPrefix: string, fileType: string, filePath: string, error: NodeJS.ErrnoException, consoleOut = false): void {
- const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : '';
- if (error.code === 'ENOENT') {
- if (consoleOut) {
- console.warn(prefix + fileType + ' file ' + filePath + ' not found: ', error);
- } else {
- logger.warn(prefix + fileType + ' file ' + filePath + ' not found: %j', error);
+import chalk from 'chalk';
+
+// import { Utils, logger } from './internal';
+import { logger } from './Logger';
+import { Utils } from './Utils';
+import type { EmptyObject, FileType, HandleErrorParams, JsonType } from '../types';
+
+export class FileUtils {
+ private constructor() {
+ // This is intentional
+ }
+
+ public static watchJsonFile<T extends JsonType>(
+ file: string,
+ fileType: FileType,
+ logPrefix: string,
+ refreshedVariable?: T,
+ listener: fs.WatchListener<string> = (event, filename) => {
+ if (Utils.isNotEmptyString(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(file, fileType, error as NodeJS.ErrnoException, logPrefix, {
+ throwError: false,
+ });
+ }