Skip to content

Commit 471e565

Browse files
committed
errorMessageAndExit
1 parent e9a5b1f commit 471e565

File tree

4 files changed

+44
-31
lines changed

4 files changed

+44
-31
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"dist/"
5252
],
5353
"dependencies": {
54+
"colors": "1.4.0",
5455
"enquirer": "2.3.5",
5556
"path-exists": "4.0.0",
5657
"recursive-copy": "2.0.10",

src/GenerateTemplateFiles.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import IReplacer from './models/IReplacer';
1010
import IResults from './models/IResults';
1111
import IDefaultCaseConverter from './models/IDefaultCaseConverter';
1212
import {
13-
throwErrorIfNoConfigItems,
14-
throwErrorIfOptionNameIsNotFound,
15-
throwErrorIfNoStringOrDynamicReplacers,
16-
throwErrorIfStringReplacersDoNotMatch,
13+
errorIfNoConfigItems,
14+
errorIfOptionNameIsNotFound,
15+
errorIfNoStringOrDynamicReplacers,
16+
errorIfStringReplacersDoNotMatch,
1717
} from './utilities/CheckUtility';
1818
import IReplacerSlotQuestion from './models/IReplacerSlotQuestion';
1919
import yargs from 'yargs';
@@ -25,8 +25,8 @@ export default class GenerateTemplateFiles {
2525
* Main method to create your template files. Accepts an array of `IConfigItem` items.
2626
*/
2727
public async generate(options: IConfigItem[]): Promise<void> {
28-
throwErrorIfNoConfigItems(options);
29-
throwErrorIfNoStringOrDynamicReplacers(options);
28+
errorIfNoConfigItems(options);
29+
errorIfNoStringOrDynamicReplacers(options);
3030

3131
const selectedConfigItem: IConfigItem = await this._getSelectedItem(options);
3232
const answeredReplacers: IReplacer[] = await this._getReplacerSlotValues(selectedConfigItem);
@@ -40,8 +40,8 @@ export default class GenerateTemplateFiles {
4040
public async commandLine(options: IConfigItem[]): Promise<void> {
4141
this._isCommandLine = true;
4242

43-
throwErrorIfNoConfigItems(options);
44-
throwErrorIfNoStringOrDynamicReplacers(options);
43+
errorIfNoConfigItems(options);
44+
errorIfNoStringOrDynamicReplacers(options);
4545

4646
const [templateName = '', ...replacers] = yargs.argv._;
4747
const selectedConfigItem: IConfigItem | undefined = options.find((configItem: IConfigItem) => {
@@ -51,7 +51,7 @@ export default class GenerateTemplateFiles {
5151
);
5252
});
5353

54-
throwErrorIfOptionNameIsNotFound(selectedConfigItem, StringUtility.toCase(templateName, CaseConverterEnum.KebabCase));
54+
errorIfOptionNameIsNotFound(selectedConfigItem, StringUtility.toCase(templateName, CaseConverterEnum.KebabCase));
5555

5656
const commandLineStringReplacers: IReplacer[] = replacers.map((str: string) => {
5757
const [slot, slotValue] = str.split('=');
@@ -62,7 +62,7 @@ export default class GenerateTemplateFiles {
6262
};
6363
});
6464

65-
throwErrorIfStringReplacersDoNotMatch(selectedConfigItem, commandLineStringReplacers);
65+
errorIfStringReplacersDoNotMatch(selectedConfigItem, commandLineStringReplacers);
6666

6767
const dynamicReplacers: IReplacer[] = selectedConfigItem?.dynamicReplacers || [];
6868

src/utilities/CheckUtility.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ import IConfigItem from '../models/IConfigItem';
22
import IReplacer from '../models/IReplacer';
33
import StringUtility from './StringUtility';
44
import IReplacerSlotQuestion from '../models/IReplacerSlotQuestion';
5+
import colors from 'colors';
6+
7+
/**
8+
* https://timber.io/blog/creating-a-real-world-cli-app-with-node/#errors-and-exit-codes
9+
* @param message
10+
* @param isError
11+
*/
12+
export const errorMessageAndExit = (isError: boolean, message: string) => {
13+
if (isError) {
14+
console.error(colors.bold.red(`[Error in generate-template-files]:`), colors.red(message));
15+
process.exit(1);
16+
}
17+
};
518

619
export const isBooleanType = (value: any) => {
720
return typeof value === 'boolean';
@@ -22,26 +35,23 @@ export const errorIfFalse = (isError: boolean, errorMessage: string): Error | vo
2235
}
2336
};
2437

25-
export const throwErrorIfNoConfigItems = (options: IConfigItem[]) => {
38+
export const errorIfNoConfigItems = (options: IConfigItem[]) => {
2639
const hasAtLeastOneItem = Boolean(options?.length);
2740

28-
if (!hasAtLeastOneItem) {
29-
throw new Error('There was no IConfigItem items found.');
30-
}
41+
errorMessageAndExit(!hasAtLeastOneItem, 'There was no IConfigItem items found.');
3142
};
3243

33-
export const throwErrorIfOptionNameIsNotFound = (item: IConfigItem | undefined, templateName: string) => {
34-
if (!item) {
35-
throw new Error(`No IConfigItem found for ${templateName}`);
36-
}
44+
export const errorIfOptionNameIsNotFound = (item: IConfigItem | undefined, templateName: string) => {
45+
errorMessageAndExit(!item, `No IConfigItem found for ${templateName}`);
3746
};
3847

39-
export const throwErrorIfStringReplacersDoNotMatch = (item: IConfigItem | undefined, commandLineStringReplacers: IReplacer[]) => {
48+
export const errorIfStringReplacersDoNotMatch = (item: IConfigItem | undefined, commandLineStringReplacers: IReplacer[]) => {
4049
const configItemStringReplacers: (string | IReplacerSlotQuestion)[] = item?.stringReplacers ?? [];
4150

42-
if (commandLineStringReplacers.length !== configItemStringReplacers.length) {
43-
throw new Error('IConfigItem stringReplacers do not match the command line arguments.');
44-
}
51+
errorMessageAndExit(
52+
commandLineStringReplacers.length !== configItemStringReplacers.length,
53+
'IConfigItem stringReplacers do not match the command line arguments.'
54+
);
4555

4656
const configItemStringReplacersKeys = configItemStringReplacers
4757
.map((replacer: string | IReplacerSlotQuestion) => {
@@ -55,20 +65,17 @@ export const throwErrorIfStringReplacersDoNotMatch = (item: IConfigItem | undefi
5565
.sort()
5666
.join(', ');
5767

58-
if (configItemStringReplacersKeys !== commandLineStringReplacersKeys) {
59-
throw new Error(
60-
` ${configItemStringReplacersKeys} does not match ${commandLineStringReplacersKeys}. IConfigItem stringReplacers do not match the command line arguments.`
61-
);
62-
}
68+
errorMessageAndExit(
69+
configItemStringReplacersKeys !== commandLineStringReplacersKeys,
70+
`${configItemStringReplacersKeys} does not match ${commandLineStringReplacersKeys}. IConfigItem stringReplacers do not match the command line arguments.`
71+
);
6372
};
6473

65-
export const throwErrorIfNoStringOrDynamicReplacers = (options: IConfigItem[]) => {
74+
export const errorIfNoStringOrDynamicReplacers = (options: IConfigItem[]) => {
6675
const hasStringOrDynamicReplacers =
6776
options.every((item: IConfigItem) => {
6877
return Boolean(item?.stringReplacers?.length) || Boolean(item?.dynamicReplacers?.length);
6978
}) && options.length > 0;
7079

71-
if (!hasStringOrDynamicReplacers) {
72-
throw new Error('IConfigItem needs to have a stringReplacers or dynamicReplacers.');
73-
}
80+
errorMessageAndExit(!hasStringOrDynamicReplacers, 'IConfigItem needs to have a stringReplacers or dynamicReplacers.');
7481
};

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,6 +1880,11 @@ color-name@~1.1.4:
18801880
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
18811881
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==
18821882

1883+
colors@1.4.0:
1884+
version "1.4.0"
1885+
resolved "https://registry.yarnpkg.com/colors/-/colors-1.4.0.tgz#c50491479d4c1bdaed2c9ced32cf7c7dc2360f78"
1886+
integrity sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==
1887+
18831888
combined-stream@^1.0.6, combined-stream@~1.0.6:
18841889
version "1.0.8"
18851890
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"

0 commit comments

Comments
 (0)