watchJsonFile: ensure the argument is defined before assigning it
[e-mobility-charging-stations-simulator.git] / src / utils / FileUtils.ts
CommitLineData
a95873d8
JB
1import { JsonType, JsonValue } from '../types/JsonType';
2
717c1e56 3import { EmptyObject } from '../types/EmptyObject';
a95873d8 4import { FileType } from '../types/FileType';
e0a50bcd 5import { HandleErrorParams } from '../types/Error';
e8191622 6import Utils from './Utils';
8eac9a09 7import chalk from 'chalk';
a95873d8 8import fs from 'fs';
9f2e3130 9import logger from './Logger';
23132a44
JB
10
11export default class FileUtils {
a95873d8
JB
12 static watchJsonFile<T extends JsonType | JsonValue>(
13 logPrefix: string,
14 fileType: FileType,
15 file: string,
16 attribute?: T,
17 listener: fs.WatchListener<string> = (event, filename) => {
18 if (filename && event === 'change') {
19 try {
20 logger.debug(logPrefix + ' ' + fileType + ' file ' + file + ' have changed, reload');
69d65c15 21 attribute && (attribute = JSON.parse(fs.readFileSync(file, 'utf8')) as T);
a95873d8
JB
22 } catch (error) {
23 FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, {
24 throwError: false,
25 });
26 }
27 }
28 }
29 ) {
30 if (file) {
31 try {
32 fs.watch(file, listener);
33 } catch (error) {
34 FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, {
35 throwError: false,
36 });
37 }
38 } else {
39 logger.info(`${logPrefix} No ${fileType} file to watch given. Not monitoring its changes`);
40 }
41 }
42
e7aeea18
JB
43 static handleFileException(
44 logPrefix: string,
a95873d8
JB
45 fileType: FileType,
46 file: string,
e7aeea18
JB
47 error: NodeJS.ErrnoException,
48 params: HandleErrorParams<EmptyObject> = { throwError: true, consoleOut: false }
49 ): void {
e8191622 50 const prefix = !Utils.isEmptyString(logPrefix) ? logPrefix + ' ' : '';
23132a44 51 if (error.code === 'ENOENT') {
e0a50bcd 52 if (params?.consoleOut) {
e7aeea18 53 console.warn(
a95873d8 54 chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' not found: '),
e7aeea18
JB
55 error
56 );
23132a44 57 } else {
a95873d8 58 logger.warn(prefix + fileType + ' file ' + file + ' not found: %j', error);
23132a44 59 }
72f041bd 60 } else if (error.code === 'EEXIST') {
e0a50bcd 61 if (params?.consoleOut) {
e7aeea18 62 console.warn(
a95873d8 63 chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' already exists: '),
e7aeea18
JB
64 error
65 );
72f041bd 66 } else {
a95873d8 67 logger.warn(prefix + fileType + ' file ' + file + ' already exists: %j', error);
72f041bd
JB
68 }
69 } else if (error.code === 'EACCES') {
e0a50bcd 70 if (params?.consoleOut) {
e7aeea18 71 console.warn(
a95873d8 72 chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' access denied: '),
e7aeea18
JB
73 error
74 );
72f041bd 75 } else {
a95873d8 76 logger.warn(prefix + fileType + ' file ' + file + ' access denied: %j', error);
72f041bd 77 }
23132a44 78 } else {
e0a50bcd 79 if (params?.consoleOut) {
e7aeea18 80 console.warn(
a95873d8 81 chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' error: '),
e7aeea18
JB
82 error
83 );
23132a44 84 } else {
a95873d8 85 logger.warn(prefix + fileType + ' file ' + file + ' error: %j', error);
23132a44 86 }
e0a50bcd
JB
87 if (params?.throwError) {
88 throw error;
89 }
23132a44
JB
90 }
91 }
92}