Skip to content

Commit 43290f6

Browse files
committed
Change to throw errors and add try/catch. add display* functions
1 parent e019856 commit 43290f6

File tree

3 files changed

+72
-59
lines changed

3 files changed

+72
-59
lines changed

src/GenerateTemplateFiles.ts

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ import IReplacer from './models/IReplacer';
1010
import IResults from './models/IResults';
1111
import IDefaultCaseConverter from './models/IDefaultCaseConverter';
1212
import {
13-
errorIfNoConfigItems,
14-
errorIfOptionNameIsNotFound,
15-
errorIfNoStringOrDynamicReplacers,
16-
errorIfStringReplacersDoNotMatch,
13+
throwErrorIfNoConfigItems,
14+
throwErrorIfOptionNameIsNotFound,
15+
throwErrorIfNoStringOrDynamicReplacers,
16+
throwErrorIfStringReplacersDoNotMatch,
17+
displayError,
18+
displayWarning,
19+
displaySuccess,
1720
} from './utilities/CheckUtility';
1821
import IReplacerSlotQuestion from './models/IReplacerSlotQuestion';
1922
import yargs from 'yargs';
@@ -25,13 +28,17 @@ export default class GenerateTemplateFiles {
2528
* Main method to create your template files. Accepts an array of `IConfigItem` items.
2629
*/
2730
public async generate(options: IConfigItem[]): Promise<void> {
28-
errorIfNoConfigItems(options);
29-
errorIfNoStringOrDynamicReplacers(options);
31+
try {
32+
throwErrorIfNoConfigItems(options);
33+
throwErrorIfNoStringOrDynamicReplacers(options);
3034

31-
const selectedConfigItem: IConfigItem = await this._getSelectedItem(options);
32-
const answeredReplacers: IReplacer[] = await this._getReplacerSlotValues(selectedConfigItem);
35+
const selectedConfigItem: IConfigItem = await this._getSelectedItem(options);
36+
const answeredReplacers: IReplacer[] = await this._getReplacerSlotValues(selectedConfigItem);
3337

34-
await this._outputFiles(selectedConfigItem, answeredReplacers);
38+
await this._outputFiles(selectedConfigItem, answeredReplacers);
39+
} catch (error) {
40+
displayError(error.message);
41+
}
3542
}
3643

3744
/**
@@ -40,33 +47,37 @@ export default class GenerateTemplateFiles {
4047
public async commandLine(options: IConfigItem[]): Promise<void> {
4148
this._isCommandLine = true;
4249

43-
errorIfNoConfigItems(options);
44-
errorIfNoStringOrDynamicReplacers(options);
50+
try {
51+
throwErrorIfNoConfigItems(options);
52+
throwErrorIfNoStringOrDynamicReplacers(options);
4553

46-
const [templateName = '', ...replacers] = yargs.argv._;
47-
const selectedConfigItem: IConfigItem | undefined = options.find((configItem: IConfigItem) => {
48-
return (
49-
StringUtility.toCase(configItem.option, CaseConverterEnum.KebabCase) ===
50-
StringUtility.toCase(templateName, CaseConverterEnum.KebabCase)
51-
);
52-
});
54+
const [templateName = '', ...replacers] = yargs.argv._;
55+
const selectedConfigItem: IConfigItem | undefined = options.find((configItem: IConfigItem) => {
56+
return (
57+
StringUtility.toCase(configItem.option, CaseConverterEnum.KebabCase) ===
58+
StringUtility.toCase(templateName, CaseConverterEnum.KebabCase)
59+
);
60+
});
5361

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

56-
const commandLineStringReplacers: IReplacer[] = replacers.map((str: string) => {
57-
const [slot, slotValue] = str.split('=');
64+
const commandLineStringReplacers: IReplacer[] = replacers.map((str: string) => {
65+
const [slot, slotValue] = str.split('=');
5866

59-
return {
60-
slot,
61-
slotValue,
62-
};
63-
});
67+
return {
68+
slot,
69+
slotValue,
70+
};
71+
});
6472

65-
errorIfStringReplacersDoNotMatch(selectedConfigItem, commandLineStringReplacers);
73+
throwErrorIfStringReplacersDoNotMatch(selectedConfigItem, commandLineStringReplacers);
6674

67-
const dynamicReplacers: IReplacer[] = selectedConfigItem?.dynamicReplacers || [];
75+
const dynamicReplacers: IReplacer[] = selectedConfigItem?.dynamicReplacers || [];
6876

69-
await this._outputFiles(selectedConfigItem!, [...commandLineStringReplacers, ...dynamicReplacers]);
77+
await this._outputFiles(selectedConfigItem!, [...commandLineStringReplacers, ...dynamicReplacers]);
78+
} catch (error) {
79+
displayError(error.message);
80+
}
7081
}
7182

7283
private async _outputFiles(selectedConfigItem: IConfigItem, replacers: IReplacer[]): Promise<void> {
@@ -77,10 +88,10 @@ export default class GenerateTemplateFiles {
7788
const shouldWriteFiles: boolean = await this._shouldWriteFiles(outputPath, selectedConfigItem);
7889

7990
if (shouldWriteFiles === false) {
80-
console.info('No new files created');
91+
displayWarning('No new files created');
8192

8293
if (this._isCommandLine) {
83-
console.info('Use --overwrite option to overwrite existing files');
94+
displayWarning('Use --overwrite option to overwrite existing files');
8495
}
8596

8697
return;
@@ -288,11 +299,11 @@ export default class GenerateTemplateFiles {
288299
try {
289300
await recursiveCopy(entryFolderPath, outputPath, recursiveCopyOptions);
290301

291-
console.info(`Files saved to: '${outputPath}'`);
302+
displaySuccess(`Files saved to: '${outputPath}'`);
292303

293304
return outputtedFilesAndFolders.filter(Boolean);
294305
} catch (error) {
295-
console.error(`Copy failed: ${error}`);
306+
displayError(`Copy failed: ${error}`);
296307

297308
return [`Copy failed: ${error}`];
298309
}

src/utilities/CheckUtility.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('displayError', () => {
77
test('should throw an error if condition is false', () => {
88
console.info = jest.fn();
99

10-
displayError(true, errorString);
10+
displayError(errorString);
1111

1212
expect(console.info).toHaveBeenCalledWith(colors.bold.red(`[Error in generate-template-files]: ${colors.red(errorString)}`));
1313
});

src/utilities/CheckUtility.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,38 @@ import StringUtility from './StringUtility';
44
import IReplacerSlotQuestion from '../models/IReplacerSlotQuestion';
55
import colors from 'colors';
66

7-
export const isBooleanType = (value: unknown): value is boolean => {
8-
return typeof value === 'boolean';
7+
export const displayError = (message: string): Error | void => {
8+
console.info(colors.bold.red(`[Error in generate-template-files]: ${colors.red(message)}`));
99
};
1010

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(colors.bold.red(`[Error in generate-template-files]: ${colors.red(error.message)}`));
17-
}
18-
}
11+
export const displayWarning = (message: string): Error | void => {
12+
console.info(colors.bold.yellow(colors.yellow(message)));
13+
};
14+
15+
export const displaySuccess = (message: string): Error | void => {
16+
console.info(colors.bold.green(colors.green(message)));
1917
};
2018

21-
export const errorIfNoConfigItems = (options: IConfigItem[]) => {
19+
export const throwErrorIfNoConfigItems = (options: IConfigItem[]) => {
2220
const hasAtLeastOneItem = Boolean(options?.length);
2321

24-
displayError(!hasAtLeastOneItem, 'There was no IConfigItem items found.');
22+
if (!hasAtLeastOneItem) {
23+
throw new Error('There was no IConfigItem items found.');
24+
}
2525
};
2626

27-
export const errorIfOptionNameIsNotFound = (item: IConfigItem | undefined, templateName: string) => {
28-
displayError(!item, `No IConfigItem found for ${templateName}`);
27+
export const throwErrorIfOptionNameIsNotFound = (item: IConfigItem | undefined, templateName: string) => {
28+
if (!item) {
29+
throw new Error(`No IConfigItem found for ${templateName}`);
30+
}
2931
};
3032

31-
export const errorIfStringReplacersDoNotMatch = (item: IConfigItem | undefined, commandLineStringReplacers: IReplacer[]) => {
33+
export const throwErrorIfStringReplacersDoNotMatch = (item: IConfigItem | undefined, commandLineStringReplacers: IReplacer[]) => {
3234
const configItemStringReplacers: (string | IReplacerSlotQuestion)[] = item?.stringReplacers ?? [];
3335

34-
displayError(
35-
commandLineStringReplacers.length !== configItemStringReplacers.length,
36-
'IConfigItem stringReplacers do not match the command line arguments.'
37-
);
36+
if (commandLineStringReplacers.length !== configItemStringReplacers.length) {
37+
throw new Error('IConfigItem stringReplacers do not match the command line arguments.');
38+
}
3839

3940
const configItemStringReplacersKeys = configItemStringReplacers
4041
.map((replacer: string | IReplacerSlotQuestion) => {
@@ -48,17 +49,18 @@ export const errorIfStringReplacersDoNotMatch = (item: IConfigItem | undefined,
4849
.sort()
4950
.join(', ');
5051

51-
displayError(
52-
configItemStringReplacersKeys !== commandLineStringReplacersKeys,
53-
`${configItemStringReplacersKeys} does not match ${commandLineStringReplacersKeys}.`
54-
);
52+
if (configItemStringReplacersKeys !== commandLineStringReplacersKeys) {
53+
throw new Error(`${configItemStringReplacersKeys} does not match ${commandLineStringReplacersKeys}.`);
54+
}
5555
};
5656

57-
export const errorIfNoStringOrDynamicReplacers = (options: IConfigItem[]) => {
57+
export const throwErrorIfNoStringOrDynamicReplacers = (options: IConfigItem[]) => {
5858
const hasStringOrDynamicReplacers =
5959
options.every((item: IConfigItem) => {
6060
return Boolean(item?.stringReplacers?.length) || Boolean(item?.dynamicReplacers?.length);
6161
}) && options.length > 0;
6262

63-
displayError(!hasStringOrDynamicReplacers, 'IConfigItem needs to have a stringReplacers or dynamicReplacers.');
63+
if (!hasStringOrDynamicReplacers) {
64+
throw new Error('IConfigItem needs to have a stringReplacers or dynamicReplacers.');
65+
}
6466
};

0 commit comments

Comments
 (0)