@@ -5,12 +5,16 @@ import through from 'through2';
55import replaceString from 'replace-string' ;
66import StringUtility from './utilities/StringUtility' ;
77import CaseConverterEnum from './constants/CaseConverterEnum' ;
8- import get from 'lodash.get' ;
98import IConfigItem from './models/IConfigItem' ;
109import IReplacer from './models/IReplacer' ;
1110import IResults from './models/IResults' ;
1211import IDefaultCaseConverter from './models/IDefaultCaseConverter' ;
13- import CheckUtility from './utilities/CheckUtility' ;
12+ import {
13+ throwErrorIfNoConfigItems ,
14+ throwErrorIfOptionNameIsNotFound ,
15+ throwErrorIfNoStringOrDynamicReplacers ,
16+ throwErrorIfStringReplacersDoNotMatch ,
17+ } from './utilities/CheckUtility' ;
1418import IReplacerSlotQuestion from './models/IReplacerSlotQuestion' ;
1519import yargs from 'yargs' ;
1620
@@ -21,6 +25,9 @@ export default class GenerateTemplateFiles {
2125 * Main method to create your template files. Accepts an array of `IConfigItem` items.
2226 */
2327 public async generate ( options : IConfigItem [ ] ) : Promise < void > {
28+ throwErrorIfNoConfigItems ( options ) ;
29+ throwErrorIfNoStringOrDynamicReplacers ( options ) ;
30+
2431 const selectedConfigItem : IConfigItem = await this . _getSelectedItem ( options ) ;
2532 const answeredReplacers : IReplacer [ ] = await this . _getReplacerSlotValues ( selectedConfigItem ) ;
2633
@@ -31,48 +38,38 @@ export default class GenerateTemplateFiles {
3138 * Main method to create your template files with the command line.
3239 */
3340 public async commandLine ( options : IConfigItem [ ] ) : Promise < void > {
34- const commandLineArgs : string [ ] = yargs . argv . _ ;
35- const templateName : string = commandLineArgs . shift ( ) ?? '' ;
36- const slots : string [ ] = commandLineArgs ;
41+ this . _isCommandLine = true ;
42+
43+ throwErrorIfNoConfigItems ( options ) ;
44+ throwErrorIfNoStringOrDynamicReplacers ( options ) ;
3745
46+ const [ templateName = '' , ...replacers ] = yargs . argv . _ ;
3847 const selectedConfigItem : IConfigItem | undefined = options . find ( ( configItem : IConfigItem ) => {
3948 return (
4049 StringUtility . toCase ( configItem . option , CaseConverterEnum . KebabCase ) ===
4150 StringUtility . toCase ( templateName , CaseConverterEnum . KebabCase )
4251 ) ;
4352 } ) ;
4453
45- if ( ! selectedConfigItem ) {
46- CheckUtility . check ( Boolean ( selectedConfigItem ) , `There was no IConfigItem found for ${ templateName } ` ) ;
54+ throwErrorIfOptionNameIsNotFound ( selectedConfigItem , StringUtility . toCase ( templateName , CaseConverterEnum . KebabCase ) ) ;
4755
48- return ;
49- }
50-
51- const replacers : IReplacer [ ] = slots . map ( ( str : string ) => {
56+ const commandLineStringReplacers : IReplacer [ ] = replacers . map ( ( str : string ) => {
5257 const [ slot , slotValue ] = str . split ( '=' ) ;
5358
54- const isValidReplacer : boolean = Boolean ( slot ) && str . includes ( '=' ) ;
55- CheckUtility . check ( isValidReplacer , `${ str } is not valid as a IReplacer` ) ;
56-
5759 return {
5860 slot,
5961 slotValue,
6062 } ;
6163 } ) ;
62- const dynamicReplacers : IReplacer [ ] = selectedConfigItem . dynamicReplacers || [ ] ;
6364
64- await this . _outputFiles ( selectedConfigItem , [ ...replacers , ...dynamicReplacers ] ) ;
65- }
66-
67- private async _outputFiles ( selectedConfigItem : IConfigItem , replacers : IReplacer [ ] ) : Promise < void > {
68- const hasStringOrDynamicReplacers : boolean = replacers . length > 0 ;
65+ throwErrorIfStringReplacersDoNotMatch ( selectedConfigItem , commandLineStringReplacers ) ;
6966
70- if ( ! hasStringOrDynamicReplacers ) {
71- CheckUtility . check ( hasStringOrDynamicReplacers , 'You need at least one IReplacer' ) ;
67+ const dynamicReplacers : IReplacer [ ] = selectedConfigItem ?. dynamicReplacers || [ ] ;
7268
73- return ;
74- }
69+ await this . _outputFiles ( selectedConfigItem ! , [ ... commandLineStringReplacers , ... dynamicReplacers ] ) ;
70+ }
7571
72+ private async _outputFiles ( selectedConfigItem : IConfigItem , replacers : IReplacer [ ] ) : Promise < void > {
7673 const { contentCase, outputPathCase} = this . _getDefaultCaseConverters ( selectedConfigItem ) ;
7774 const contentReplacers : IReplacer [ ] = this . _getReplacers ( replacers , contentCase ) ;
7875 const outputPathReplacers : IReplacer [ ] = this . _getReplacers ( replacers , outputPathCase ) ;
@@ -127,12 +124,8 @@ export default class GenerateTemplateFiles {
127124 /**
128125 */
129126 private _getDefaultCaseConverters ( selectedConfigItem : IConfigItem ) : IDefaultCaseConverter {
130- const defaultContentCase : CaseConverterEnum = get ( selectedConfigItem , 'defaultCase' , CaseConverterEnum . None ) as CaseConverterEnum ;
131- const defaultOutputPath : CaseConverterEnum = get (
132- selectedConfigItem ,
133- 'output.pathAndFileNameDefaultCase' ,
134- defaultContentCase
135- ) as CaseConverterEnum ;
127+ const defaultContentCase : CaseConverterEnum = selectedConfigItem ?. defaultCase ?? CaseConverterEnum . None ;
128+ const defaultOutputPath : CaseConverterEnum = selectedConfigItem . output ?. pathAndFileNameDefaultCase ?? defaultContentCase ;
136129
137130 return {
138131 contentCase : defaultContentCase ,
0 commit comments