@@ -10,10 +10,13 @@ import IReplacer from './models/IReplacer';
1010import IResults from './models/IResults' ;
1111import IDefaultCaseConverter from './models/IDefaultCaseConverter' ;
1212import {
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' ;
1821import IReplacerSlotQuestion from './models/IReplacerSlotQuestion' ;
1922import 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,39 +47,45 @@ 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 ) ;
53+
54+ const [ templateName = '' , ...replacers ] = yargs . argv . _ ;
55+ const selectedConfigItem : IConfigItem | undefined = options . find (
56+ ( configItem : IConfigItem ) => {
57+ return (
58+ StringUtility . toCase ( configItem . option , CaseConverterEnum . KebabCase ) ===
59+ StringUtility . toCase ( templateName , CaseConverterEnum . KebabCase )
60+ ) ;
61+ }
62+ ) ;
4563
46- const [ templateName = '' , ...replacers ] = yargs . argv . _ ;
47- const selectedConfigItem : IConfigItem | undefined = options . find ( ( configItem : IConfigItem ) => {
48- return (
49- StringUtility . toCase ( configItem . option , CaseConverterEnum . KebabCase ) ===
64+ throwErrorIfOptionNameIsNotFound (
65+ selectedConfigItem ,
5066 StringUtility . toCase ( templateName , CaseConverterEnum . KebabCase )
5167 ) ;
52- } ) ;
53-
54- errorIfOptionNameIsNotFound (
55- selectedConfigItem ,
56- StringUtility . toCase ( templateName , CaseConverterEnum . KebabCase )
57- ) ;
5868
59- const commandLineStringReplacers : IReplacer [ ] = replacers . map ( ( str : string ) => {
60- const [ slot , slotValue ] = str . split ( '=' ) ;
69+ const commandLineStringReplacers : IReplacer [ ] = replacers . map ( ( str : string ) => {
70+ const [ slot , slotValue ] = str . split ( '=' ) ;
6171
62- return {
63- slot,
64- slotValue,
65- } ;
66- } ) ;
72+ return {
73+ slot,
74+ slotValue,
75+ } ;
76+ } ) ;
6777
68- errorIfStringReplacersDoNotMatch ( selectedConfigItem , commandLineStringReplacers ) ;
78+ throwErrorIfStringReplacersDoNotMatch ( selectedConfigItem , commandLineStringReplacers ) ;
6979
70- const dynamicReplacers : IReplacer [ ] = selectedConfigItem ?. dynamicReplacers || [ ] ;
80+ const dynamicReplacers : IReplacer [ ] = selectedConfigItem ?. dynamicReplacers || [ ] ;
7181
72- await this . _outputFiles ( selectedConfigItem ! , [
73- ...commandLineStringReplacers ,
74- ...dynamicReplacers ,
75- ] ) ;
82+ await this . _outputFiles ( selectedConfigItem ! , [
83+ ...commandLineStringReplacers ,
84+ ...dynamicReplacers ,
85+ ] ) ;
86+ } catch ( error ) {
87+ displayError ( error . message ) ;
88+ }
7689 }
7790
7891 private async _outputFiles (
@@ -86,10 +99,10 @@ export default class GenerateTemplateFiles {
8699 const shouldWriteFiles : boolean = await this . _shouldWriteFiles ( outputPath , selectedConfigItem ) ;
87100
88101 if ( shouldWriteFiles === false ) {
89- console . info ( 'No new files created' ) ;
102+ displayWarning ( 'No new files created' ) ;
90103
91104 if ( this . _isCommandLine ) {
92- console . info ( 'Use --overwrite option to overwrite existing files' ) ;
105+ displayWarning ( 'Use --overwrite option to overwrite existing files' ) ;
93106 }
94107
95108 return ;
@@ -314,11 +327,11 @@ export default class GenerateTemplateFiles {
314327 try {
315328 await recursiveCopy ( entryFolderPath , outputPath , recursiveCopyOptions ) ;
316329
317- console . info ( `Files saved to: '${ outputPath } '` ) ;
330+ displaySuccess ( `Files saved to: '${ outputPath } '` ) ;
318331
319332 return outputtedFilesAndFolders . filter ( Boolean ) ;
320333 } catch ( error ) {
321- console . error ( `Copy failed: ${ error } ` ) ;
334+ displayError ( `Copy failed: ${ error } ` ) ;
322335
323336 return [ `Copy failed: ${ error } ` ] ;
324337 }
0 commit comments