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