Commit | Line | Data |
---|---|---|
8eac9a09 | 1 | import chalk from 'chalk'; |
23132a44 JB |
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) { | |
8eac9a09 | 9 | console.warn(chalk.yellow(prefix + fileType + ' file ' + filePath + ' not found: '), error); |
23132a44 | 10 | } else { |
9fbc92f7 | 11 | logger.warn(prefix + fileType + ' file ' + filePath + ' not found: %j', error); |
23132a44 | 12 | } |
72f041bd JB |
13 | } else if (error.code === 'EEXIST') { |
14 | if (consoleOut) { | |
8eac9a09 | 15 | console.warn(chalk.yellow(prefix + fileType + ' file ' + filePath + ' already exists: '), error); |
72f041bd JB |
16 | } else { |
17 | logger.warn(prefix + fileType + ' file ' + filePath + ' already exists: %j', error); | |
18 | } | |
19 | } else if (error.code === 'EACCES') { | |
20 | if (consoleOut) { | |
8eac9a09 | 21 | console.warn(chalk.yellow(prefix + fileType + ' file ' + filePath + ' access denied: '), error); |
72f041bd JB |
22 | } else { |
23 | logger.warn(prefix + fileType + ' file ' + filePath + ' access denied: %j', error); | |
24 | } | |
23132a44 JB |
25 | } else { |
26 | if (consoleOut) { | |
531c5e7a | 27 | console.warn(chalk.yellow(prefix + fileType + ' file ' + filePath + ' error: '), error); |
23132a44 | 28 | } else { |
531c5e7a | 29 | logger.warn(prefix + fileType + ' file ' + filePath + ' error: %j', error); |
23132a44 JB |
30 | } |
31 | throw error; | |
32 | } | |
33 | } | |
34 | } |