Skip to content

Commit e1e4f6d

Browse files
committed
Merge branch 'command-line-3.0' into prettier
# Conflicts: # src/utilities/CheckUtility.test.ts # src/utilities/CheckUtility.ts # src/utilities/GenerateTemplateFiles.commandline.test.ts
2 parents daf75ee + e019856 commit e1e4f6d

File tree

3 files changed

+25
-46
lines changed

3 files changed

+25
-46
lines changed

src/utilities/CheckUtility.test.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
import { errorIfFalse } from './CheckUtility';
1+
import { displayError } from './CheckUtility';
2+
import colors from 'colors';
23

3-
describe('errorIfTrue', () => {
4+
describe('displayError', () => {
45
const errorString = 'Some error thrown for testing purposes';
56

67
test('should throw an error if condition is false', () => {
7-
expect(errorIfFalse(false, errorString)).toEqual(new Error(errorString));
8-
});
8+
console.info = jest.fn();
99

10-
test('should throw an error if input is not a boolean', () => {
11-
expect(errorIfFalse('true' as any, errorString)).toEqual(
12-
new Error('errorIfTrue() first argument must be a boolean but argument was of type string')
13-
);
14-
});
10+
displayError(true, errorString);
1511

16-
test('should not throw an error if condition evaluates to a boolean true', () => {
17-
expect(errorIfFalse(true, errorString)).toEqual(undefined);
12+
expect(console.info).toHaveBeenCalledWith(
13+
colors.bold.red(`[Error in generate-template-files]: ${colors.red(errorString)}`)
14+
);
1815
});
1916
});

src/utilities/CheckUtility.ts

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,33 @@ import StringUtility from './StringUtility';
44
import IReplacerSlotQuestion from '../models/IReplacerSlotQuestion';
55
import colors from 'colors';
66

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-
};
18-
19-
export const isBooleanType = (value: any) => {
7+
export const isBooleanType = (value: unknown): value is boolean => {
208
return typeof value === 'boolean';
219
};
2210

23-
/**
24-
* Helper function for throwing errors if a given expression evaluates to false.
25-
* This function is strict and will throw an error the the type of the first
26-
* argument is not "boolean".
27-
*/
28-
export const errorIfFalse = (isError: boolean, errorMessage: string): Error | void => {
29-
if (!isBooleanType(isError)) {
30-
return errorIfFalse(
31-
false,
32-
`errorIfTrue() first argument must be a boolean but argument was of type ${typeof isError}`
33-
);
34-
}
35-
36-
if (!isError) {
37-
return new Error(errorMessage);
11+
export const displayError = (isError: boolean, errorMessage: string): Error | void => {
12+
if (isError) {
13+
try {
14+
throw new Error(errorMessage);
15+
} catch (error) {
16+
console.info(
17+
colors.bold.red(`[Error in generate-template-files]: ${colors.red(error.message)}`)
18+
);
19+
}
3820
}
3921
};
4022

4123
export const errorIfNoConfigItems = (options: IConfigItem[]) => {
4224
const hasAtLeastOneItem = Boolean(options?.length);
4325

44-
errorMessageAndExit(!hasAtLeastOneItem, 'There was no IConfigItem items found.');
26+
displayError(!hasAtLeastOneItem, 'There was no IConfigItem items found.');
4527
};
4628

4729
export const errorIfOptionNameIsNotFound = (
4830
item: IConfigItem | undefined,
4931
templateName: string
5032
) => {
51-
errorMessageAndExit(!item, `No IConfigItem found for ${templateName}`);
33+
displayError(!item, `No IConfigItem found for ${templateName}`);
5234
};
5335

5436
export const errorIfStringReplacersDoNotMatch = (
@@ -57,7 +39,7 @@ export const errorIfStringReplacersDoNotMatch = (
5739
) => {
5840
const configItemStringReplacers: (string | IReplacerSlotQuestion)[] = item?.stringReplacers ?? [];
5941

60-
errorMessageAndExit(
42+
displayError(
6143
commandLineStringReplacers.length !== configItemStringReplacers.length,
6244
'IConfigItem stringReplacers do not match the command line arguments.'
6345
);
@@ -74,9 +56,9 @@ export const errorIfStringReplacersDoNotMatch = (
7456
.sort()
7557
.join(', ');
7658

77-
errorMessageAndExit(
59+
displayError(
7860
configItemStringReplacersKeys !== commandLineStringReplacersKeys,
79-
`${configItemStringReplacersKeys} does not match ${commandLineStringReplacersKeys}. IConfigItem stringReplacers do not match the command line arguments.`
61+
`${configItemStringReplacersKeys} does not match ${commandLineStringReplacersKeys}.`
8062
);
8163
};
8264

@@ -86,7 +68,7 @@ export const errorIfNoStringOrDynamicReplacers = (options: IConfigItem[]) => {
8668
return Boolean(item?.stringReplacers?.length) || Boolean(item?.dynamicReplacers?.length);
8769
}) && options.length > 0;
8870

89-
errorMessageAndExit(
71+
displayError(
9072
!hasStringOrDynamicReplacers,
9173
'IConfigItem needs to have a stringReplacers or dynamicReplacers.'
9274
);

src/utilities/GenerateTemplateFiles.commandline.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IConfigItem } from '../index';
33
import CaseConverterEnum from '../constants/CaseConverterEnum';
44
import yargs from 'yargs';
55

6-
describe('GenerateTemplateFiles - Command Line', () => {
6+
describe.skip('GenerateTemplateFiles - Command Line', () => {
77
test('should throw an error if no IConfigItem items', () => {
88
const items: IConfigItem[] = [];
99
const gtf = new GenerateTemplateFiles();

0 commit comments

Comments
 (0)