Skip to content

Commit aa64f27

Browse files
committed
feature: Exit with proper codes on error
1 parent 6a77206 commit aa64f27

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Diff to Html generates pretty HTML diffs from unified and git diff output in you
2323
- [Distributions](#distributions)
2424
- [Setup](#setup)
2525
- [Usage](#usage)
26+
- [Exit Status Codes](#exit-status-codes)
2627
- [Custom HTML wrapper template](#custom-html-wrapper-template)
2728
- [Examples](#examples)
2829
- [Contribute](#contribute)
@@ -89,6 +90,14 @@ Usage: diff2html [ flags and/or options ] -- [git diff passthrough flags and opt
8990
| -v | --version | Show version number | | |
9091
| -h | --help | Show help | | |
9192

93+
94+
## Exit Status Codes
95+
96+
* :tada: 0: Success
97+
* :dizzy_face: 1: Generic Error
98+
* :cold_sweat: 3: Input diff is empty
99+
* :cop: 4: Value of `--hwt | --htmlWrapperTemplate` is not a valid file
100+
92101
### Custom HTML wrapper template
93102

94103
The template is a very based on a simple replace of several placeholders as coded

src/cli.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,21 @@ export async function getInput(inputType: InputType, inputArgs: string[], ignore
8080

8181
export function getOutput(options: Diff2HtmlConfig, config: Configuration, input: string): string {
8282
if (config.htmlWrapperTemplate && !fs.existsSync(config.htmlWrapperTemplate)) {
83+
process.exitCode = 4;
8384
throw new Error(`Template ('${config.htmlWrapperTemplate}') not found!`);
8485
}
8586

8687
const diffJson = parse(input, options);
8788

88-
if (config.formatType === 'html') {
89-
const htmlContent = html(diffJson, { ...options });
90-
return prepareHTML(htmlContent, config);
91-
} else if (config.formatType === 'json') {
92-
return JSON.stringify(diffJson);
89+
switch (config.formatType) {
90+
case 'html': {
91+
const htmlContent = html(diffJson, { ...options });
92+
return prepareHTML(htmlContent, config);
93+
}
94+
case 'json': {
95+
return JSON.stringify(diffJson);
96+
}
9397
}
94-
95-
throw new Error(`Wrong output format '${config.formatType}'!`);
9698
}
9799

98100
export function preview(content: string, format: string): void {

src/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export async function main(): Promise<void> {
1212
const input = await cli.getInput(configuration.inputSource, argv.extraArguments, configuration.ignore);
1313

1414
if (!input) {
15+
process.exitCode = 3;
1516
log.error('The input is empty. Try again.');
1617
yargs.help();
1718
return;
@@ -32,6 +33,9 @@ export async function main(): Promise<void> {
3233
log.print(output);
3334
}
3435
} catch (error) {
36+
if (process.exitCode === undefined || process.exitCode === 0) {
37+
process.exitCode = 1;
38+
}
3539
log.error(error);
3640
}
3741
}

0 commit comments

Comments
 (0)