| 1 | import fs from 'fs'; |
| 2 | |
| 3 | import chalk from 'chalk'; |
| 4 | |
| 5 | import type { EmptyObject } from '../types/EmptyObject'; |
| 6 | import type { HandleErrorParams } from '../types/Error'; |
| 7 | import type { FileType } from '../types/FileType'; |
| 8 | import type { JsonType } from '../types/JsonType'; |
| 9 | import logger from './Logger'; |
| 10 | import Utils from './Utils'; |
| 11 | |
| 12 | export default class FileUtils { |
| 13 | private constructor() { |
| 14 | // This is intentional |
| 15 | } |
| 16 | |
| 17 | public static watchJsonFile<T extends JsonType>( |
| 18 | logPrefix: string, |
| 19 | fileType: FileType, |
| 20 | file: string, |
| 21 | refreshedVariable?: T, |
| 22 | listener: fs.WatchListener<string> = (event, filename) => { |
| 23 | if (filename && event === 'change') { |
| 24 | try { |
| 25 | logger.debug(logPrefix + ' ' + fileType + ' file ' + file + ' have changed, reload'); |
| 26 | refreshedVariable && (refreshedVariable = JSON.parse(fs.readFileSync(file, 'utf8')) as T); |
| 27 | } catch (error) { |
| 28 | FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, { |
| 29 | throwError: false, |
| 30 | }); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | ): fs.FSWatcher { |
| 35 | if (file) { |
| 36 | try { |
| 37 | return fs.watch(file, listener); |
| 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 | |
| 48 | public static handleFileException( |
| 49 | logPrefix: string, |
| 50 | fileType: FileType, |
| 51 | file: string, |
| 52 | error: NodeJS.ErrnoException, |
| 53 | params: HandleErrorParams<EmptyObject> = { throwError: true, consoleOut: false } |
| 54 | ): void { |
| 55 | const prefix = !Utils.isEmptyString(logPrefix) ? logPrefix + ' ' : ''; |
| 56 | if (error.code === 'ENOENT') { |
| 57 | if (params?.consoleOut) { |
| 58 | console.warn( |
| 59 | chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' not found: '), |
| 60 | error |
| 61 | ); |
| 62 | } else { |
| 63 | logger.warn(prefix + fileType + ' file ' + file + ' not found:', error); |
| 64 | } |
| 65 | } else if (error.code === 'EEXIST') { |
| 66 | if (params?.consoleOut) { |
| 67 | console.warn( |
| 68 | chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' already exists: '), |
| 69 | error |
| 70 | ); |
| 71 | } else { |
| 72 | logger.warn(prefix + fileType + ' file ' + file + ' already exists:', error); |
| 73 | } |
| 74 | } else if (error.code === 'EACCES') { |
| 75 | if (params?.consoleOut) { |
| 76 | console.warn( |
| 77 | chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' access denied: '), |
| 78 | error |
| 79 | ); |
| 80 | } else { |
| 81 | logger.warn(prefix + fileType + ' file ' + file + ' access denied:', error); |
| 82 | } |
| 83 | } else { |
| 84 | if (params?.consoleOut) { |
| 85 | console.warn( |
| 86 | chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' error: '), |
| 87 | error |
| 88 | ); |
| 89 | } else { |
| 90 | logger.warn(prefix + fileType + ' file ' + file + ' error:', error); |
| 91 | } |
| 92 | if (params?.throwError) { |
| 93 | throw error; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |