-import { JsonObject, JsonType } from '../types/JsonType';
+import fs from 'fs';
-import { EmptyObject } from '../types/EmptyObject';
-import { FileType } from '../types/FileType';
-import { HandleErrorParams } from '../types/Error';
-import Utils from './Utils';
import chalk from 'chalk';
-import fs from 'fs';
+
+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<T extends JsonObject | JsonType>(
+ private constructor() {
+ // This is intentional
+ }
+
+ public static watchJsonFile<T extends JsonType>(
logPrefix: string,
fileType: FileType,
file: string,
- attribute?: T,
+ refreshedVariable?: 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);
+ 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 {
- fs.watch(file, listener);
+ return fs.watch(file, listener);
} catch (error) {
FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, {
throwError: false,
}
}
- static handleFileException(
+ public static handleFileException(
logPrefix: string,
fileType: FileType,
file: string,
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) {
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) {
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) {
error
);
} else {
- logger.warn(prefix + fileType + ' file ' + file + ' error: %j', error);
+ logger.warn(prefix + fileType + ' file ' + file + ' error:', error);
}
if (params?.throwError) {
throw error;