Skip to content

Commit

Permalink
Use proper exit codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim Biesbjerg committed Mar 25, 2020
1 parent a83123f commit 9908681
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
14 changes: 11 additions & 3 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
}
26 changes: 14 additions & 12 deletions src/cli/tasks/extract.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9908681

Please sign in to comment.