| 1 | import chalk from 'chalk'; |
| 2 | import logger from './Logger'; |
| 3 | |
| 4 | export default class FileUtils { |
| 5 | static handleFileException(logPrefix: string, fileType: string, filePath: string, error: NodeJS.ErrnoException, consoleOut = false): void { |
| 6 | const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : ''; |
| 7 | if (error.code === 'ENOENT') { |
| 8 | if (consoleOut) { |
| 9 | console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' not found: '), error); |
| 10 | } else { |
| 11 | logger.warn(prefix + fileType + ' file ' + filePath + ' not found: %j', error); |
| 12 | } |
| 13 | } else if (error.code === 'EEXIST') { |
| 14 | if (consoleOut) { |
| 15 | console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' already exists: '), error); |
| 16 | } else { |
| 17 | logger.warn(prefix + fileType + ' file ' + filePath + ' already exists: %j', error); |
| 18 | } |
| 19 | } else if (error.code === 'EACCES') { |
| 20 | if (consoleOut) { |
| 21 | console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' access denied: '), error); |
| 22 | } else { |
| 23 | logger.warn(prefix + fileType + ' file ' + filePath + ' access denied: %j', error); |
| 24 | } |
| 25 | } else { |
| 26 | if (consoleOut) { |
| 27 | console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' error: '), error); |
| 28 | } else { |
| 29 | logger.warn(prefix + fileType + ' file ' + filePath + ' error: %j', error); |
| 30 | } |
| 31 | throw error; |
| 32 | } |
| 33 | } |
| 34 | } |