Commit | Line | Data |
---|---|---|
8114d10e JB |
1 | import fs from 'fs'; |
2 | ||
3 | import chalk from 'chalk'; | |
4 | ||
78202038 JB |
5 | import logger from './Logger'; |
6 | import Utils from './Utils'; | |
6c1761d4 JB |
7 | import type { EmptyObject } from '../types/EmptyObject'; |
8 | import type { HandleErrorParams } from '../types/Error'; | |
9 | import type { FileType } from '../types/FileType'; | |
10 | import type { JsonType } from '../types/JsonType'; | |
23132a44 JB |
11 | |
12 | export 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 { | |
cca3d474 | 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 + ' ' : ''; |
6d8b0b0e JB |
56 | let logMsg: string; |
57 | switch (error.code) { | |
58 | case 'ENOENT': | |
59 | logMsg = `${fileType} file ${file} not found:`; | |
60 | break; | |
61 | case 'EEXIST': | |
62 | logMsg = `${fileType} file ${file} already exists:`; | |
63 | break; | |
64 | case 'EACCES': | |
65 | logMsg = `${fileType} file ${file} access denied:`; | |
66 | break; | |
67 | default: | |
68 | logMsg = `${fileType} file ${file} error:`; | |
69 | } | |
70 | if (params?.consoleOut) { | |
71 | logMsg = `${logMsg} `; | |
72 | console.warn(`${chalk.green(prefix)}${chalk.yellow(logMsg)}`, error); | |
23132a44 | 73 | } else { |
6d8b0b0e JB |
74 | logger.warn(`${prefix}${logMsg}`, error); |
75 | } | |
76 | if (params?.throwError) { | |
77 | throw error; | |
23132a44 JB |
78 | } |
79 | } | |
80 | } |