diff --git a/src/cli/cli.ts b/src/cli/cli.ts index 506c26d..d3df4b3 100755 --- a/src/cli/cli.ts +++ b/src/cli/cli.ts @@ -1,4 +1,5 @@ import * as yargs from 'yargs'; +import { red, green } from 'colorette'; import { ExtractTask } from './tasks/extract.task'; import { ParserInterface } from '../parsers/parser.interface'; @@ -148,6 +149,13 @@ const compiler: CompilerInterface = CompilerFactory.create(cli.format, { }); extractTask.setCompiler(compiler); -extractTask.execute(); - -console.log(donateMessage); +// Run task +try { + extractTask.execute(); + console.log(green('\nDone.\n')); + console.log(donateMessage); + process.exit(0); +} catch (e) { + console.log(red(`\nAn error occurred: ${e}\n`)); + process.exit(1); +} diff --git a/src/cli/tasks/extract.task.ts b/src/cli/tasks/extract.task.ts index 18606c0..4bc2ef7 100644 --- a/src/cli/tasks/extract.task.ts +++ b/src/cli/tasks/extract.task.ts @@ -59,28 +59,30 @@ export class ExtractTask implements TaskInterface { try { existing = this.compiler.parse(fs.readFileSync(outputPath, 'utf-8')); } catch (e) { - this.out(`%s %s`, dim(`- ${outputPath}`), red(`(merge error: ${e})`)); - return; + this.out(`%s %s`, dim(`- ${outputPath}`), red(`[ERROR]`)); + throw e; } } // merge extracted strings with existing const draft = extracted.union(existing); - if (existing.isEmpty()) { - this.out(dim(`- ${outputPath}`)); - } else { - this.out(`%s %s`, dim(`- ${outputPath}`), green('(merged)')); - } - // Run collection through post processors const final = this.process(draft, extracted, existing); - // Save to file - this.save(outputPath, final); + // Save + try { + let event = 'CREATED'; + if (!fs.existsSync(outputPath)) { + this.options.replace ? event = 'REPLACED' : event = 'MERGED'; + } + this.save(outputPath, final); + this.out(`%s %s`, dim(`- ${outputPath}`), green(`[${event}]`)); + } catch (e) { + this.out(`%s %s`, dim(`- ${outputPath}`), red(`[ERROR]`)); + throw e; + } }); - - this.out(green('\nDone.\n')); } public setParsers(parsers: ParserInterface[]): this {