| 1 | import { EmptyObject } from '../types/EmptyObject'; |
| 2 | import { HandleErrorParams } from '../types/Error'; |
| 3 | import Utils from './Utils'; |
| 4 | import chalk from 'chalk'; |
| 5 | import logger from './Logger'; |
| 6 | |
| 7 | export default class FileUtils { |
| 8 | static handleFileException( |
| 9 | logPrefix: string, |
| 10 | fileType: string, |
| 11 | filePath: string, |
| 12 | error: NodeJS.ErrnoException, |
| 13 | params: HandleErrorParams<EmptyObject> = { throwError: true, consoleOut: false } |
| 14 | ): void { |
| 15 | const prefix = !Utils.isEmptyString(logPrefix) ? logPrefix + ' ' : ''; |
| 16 | if (error.code === 'ENOENT') { |
| 17 | if (params?.consoleOut) { |
| 18 | console.warn( |
| 19 | chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' not found: '), |
| 20 | error |
| 21 | ); |
| 22 | } else { |
| 23 | logger.warn(prefix + fileType + ' file ' + filePath + ' not found: %j', error); |
| 24 | } |
| 25 | } else if (error.code === 'EEXIST') { |
| 26 | if (params?.consoleOut) { |
| 27 | console.warn( |
| 28 | chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' already exists: '), |
| 29 | error |
| 30 | ); |
| 31 | } else { |
| 32 | logger.warn(prefix + fileType + ' file ' + filePath + ' already exists: %j', error); |
| 33 | } |
| 34 | } else if (error.code === 'EACCES') { |
| 35 | if (params?.consoleOut) { |
| 36 | console.warn( |
| 37 | chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' access denied: '), |
| 38 | error |
| 39 | ); |
| 40 | } else { |
| 41 | logger.warn(prefix + fileType + ' file ' + filePath + ' access denied: %j', error); |
| 42 | } |
| 43 | } else { |
| 44 | if (params?.consoleOut) { |
| 45 | console.warn( |
| 46 | chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' error: '), |
| 47 | error |
| 48 | ); |
| 49 | } else { |
| 50 | logger.warn(prefix + fileType + ' file ' + filePath + ' error: %j', error); |
| 51 | } |
| 52 | if (params?.throwError) { |
| 53 | throw error; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |