@@ -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,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 }
0 commit comments