@@ -4,51 +4,33 @@ import StringUtility from './StringUtility';
44import IReplacerSlotQuestion from '../models/IReplacerSlotQuestion' ;
55import 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
4123export 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
4729export 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
5436export 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 ) ;
0 commit comments