@@ -2,6 +2,19 @@ import IConfigItem from '../models/IConfigItem';
22import IReplacer from '../models/IReplacer' ;
33import StringUtility from './StringUtility' ;
44import 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
619export 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} ;
0 commit comments