@@ -4,51 +4,34 @@ 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 ( false , `errorIfTrue() first argument must be a boolean but argument was of type ${ typeof isError } ` ) ;
31- }
32-
33- if ( ! isError ) {
34- 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 ( colors . bold . red ( `[Error in generate-template-files]: ${ colors . red ( error . message ) } ` ) ) ;
17+ }
3518 }
3619} ;
3720
3821export const errorIfNoConfigItems = ( options : IConfigItem [ ] ) => {
3922 const hasAtLeastOneItem = Boolean ( options ?. length ) ;
4023
41- errorMessageAndExit ( ! hasAtLeastOneItem , 'There was no IConfigItem items found.' ) ;
24+ displayError ( ! hasAtLeastOneItem , 'There was no IConfigItem items found.' ) ;
4225} ;
4326
4427export const errorIfOptionNameIsNotFound = ( item : IConfigItem | undefined , templateName : string ) => {
45- errorMessageAndExit ( ! item , `No IConfigItem found for ${ templateName } ` ) ;
28+ displayError ( ! item , `No IConfigItem found for ${ templateName } ` ) ;
4629} ;
4730
4831export const errorIfStringReplacersDoNotMatch = ( item : IConfigItem | undefined , commandLineStringReplacers : IReplacer [ ] ) => {
4932 const configItemStringReplacers : ( string | IReplacerSlotQuestion ) [ ] = item ?. stringReplacers ?? [ ] ;
5033
51- errorMessageAndExit (
34+ displayError (
5235 commandLineStringReplacers . length !== configItemStringReplacers . length ,
5336 'IConfigItem stringReplacers do not match the command line arguments.'
5437 ) ;
@@ -65,9 +48,9 @@ export const errorIfStringReplacersDoNotMatch = (item: IConfigItem | undefined,
6548 . sort ( )
6649 . join ( ', ' ) ;
6750
68- errorMessageAndExit (
51+ displayError (
6952 configItemStringReplacersKeys !== commandLineStringReplacersKeys ,
70- `${ configItemStringReplacersKeys } does not match ${ commandLineStringReplacersKeys } . IConfigItem stringReplacers do not match the command line arguments. `
53+ `${ configItemStringReplacersKeys } does not match ${ commandLineStringReplacersKeys } .`
7154 ) ;
7255} ;
7356
@@ -77,5 +60,5 @@ export const errorIfNoStringOrDynamicReplacers = (options: IConfigItem[]) => {
7760 return Boolean ( item ?. stringReplacers ?. length ) || Boolean ( item ?. dynamicReplacers ?. length ) ;
7861 } ) && options . length > 0 ;
7962
80- errorMessageAndExit ( ! hasStringOrDynamicReplacers , 'IConfigItem needs to have a stringReplacers or dynamicReplacers.' ) ;
63+ displayError ( ! hasStringOrDynamicReplacers , 'IConfigItem needs to have a stringReplacers or dynamicReplacers.' ) ;
8164} ;
0 commit comments