Commit | Line | Data |
---|---|---|
130783a7 | 1 | import fs from 'node:fs'; |
8114d10e JB |
2 | |
3 | import chalk from 'chalk'; | |
4 | ||
878e026c JB |
5 | // import { Utils, logger } from './internal'; |
6 | import { logger } from './Logger'; | |
7 | import { Utils } from './Utils'; | |
268a74bb | 8 | import type { EmptyObject, FileType, HandleErrorParams, JsonType } from '../types'; |
23132a44 | 9 | |
268a74bb | 10 | export class FileUtils { |
d5bd1c00 JB |
11 | private constructor() { |
12 | // This is intentional | |
13 | } | |
14 | ||
9d7484a4 | 15 | public static watchJsonFile<T extends JsonType>( |
a95873d8 | 16 | file: string, |
7164966d JB |
17 | fileType: FileType, |
18 | logPrefix: string, | |
9d7484a4 | 19 | refreshedVariable?: T, |
a95873d8 | 20 | listener: fs.WatchListener<string> = (event, filename) => { |
5a2a53cf | 21 | if (Utils.isNotEmptyString(filename) && event === 'change') { |
a95873d8 | 22 | try { |
cca3d474 | 23 | logger.debug(`${logPrefix} ${fileType} file ${file} have changed, reload`); |
9d7484a4 | 24 | refreshedVariable && (refreshedVariable = JSON.parse(fs.readFileSync(file, 'utf8')) as T); |
a95873d8 | 25 | } catch (error) { |
7164966d | 26 | FileUtils.handleFileException(file, fileType, error as NodeJS.ErrnoException, logPrefix, { |
a95873d8 JB |
27 | throwError: false, |
28 | }); | |
29 | } | |
30 | } | |
31 | } | |
1895299d | 32 | ): fs.FSWatcher | undefined { |
5a2a53cf | 33 | if (Utils.isNotEmptyString(file)) { |
a95873d8 | 34 | try { |
9d7484a4 | 35 | return fs.watch(file, listener); |
a95873d8 | 36 | } catch (error) { |
7164966d | 37 | FileUtils.handleFileException(file, fileType, error as NodeJS.ErrnoException, logPrefix, { |
a95873d8 JB |
38 | throwError: false, |
39 | }); | |
40 | } | |
41 | } else { | |
42 | logger.info(`${logPrefix} No ${fileType} file to watch given. Not monitoring its changes`); | |
43 | } | |
44 | } | |
45 | ||
9d7484a4 | 46 | public static handleFileException( |
a95873d8 | 47 | file: string, |
7164966d | 48 | fileType: FileType, |
e7aeea18 | 49 | error: NodeJS.ErrnoException, |
7164966d | 50 | logPrefix: string, |
e7aeea18 JB |
51 | params: HandleErrorParams<EmptyObject> = { throwError: true, consoleOut: false } |
52 | ): void { | |
5a2a53cf | 53 | const prefix = Utils.isNotEmptyString(logPrefix) ? `${logPrefix} ` : ''; |
6d8b0b0e JB |
54 | let logMsg: string; |
55 | switch (error.code) { | |
56 | case 'ENOENT': | |
57 | logMsg = `${fileType} file ${file} not found:`; | |
58 | break; | |
59 | case 'EEXIST': | |
60 | logMsg = `${fileType} file ${file} already exists:`; | |
61 | break; | |
62 | case 'EACCES': | |
63 | logMsg = `${fileType} file ${file} access denied:`; | |
64 | break; | |
65 | default: | |
66 | logMsg = `${fileType} file ${file} error:`; | |
67 | } | |
68 | if (params?.consoleOut) { | |
69 | logMsg = `${logMsg} `; | |
70 | console.warn(`${chalk.green(prefix)}${chalk.yellow(logMsg)}`, error); | |
23132a44 | 71 | } else { |
6d8b0b0e JB |
72 | logger.warn(`${prefix}${logMsg}`, error); |
73 | } | |
74 | if (params?.throwError) { | |
75 | throw error; | |
23132a44 JB |
76 | } |
77 | } | |
78 | } |