1
+ import { BaseGenerator } from '@yeoman/types' ;
1
2
import { Command , Option } from 'commander' ;
2
3
3
4
export default class YeomanCommand extends Command {
4
- createCommand ( name ) {
5
+ override createCommand ( name ?: string ) {
5
6
return new YeomanCommand ( name ) ;
6
7
}
7
8
@@ -10,7 +11,7 @@ export default class YeomanCommand extends Command {
10
11
* @param {Option } option
11
12
* @return {YeomanCommand } this;
12
13
*/
13
- addOption ( option ) {
14
+ addOption ( option : Option ) {
14
15
if ( ! option . long || option . required || option . optional ) {
15
16
return super . addOption ( option ) ;
16
17
}
@@ -30,12 +31,22 @@ export default class YeomanCommand extends Command {
30
31
return result ;
31
32
}
32
33
34
+ /**
35
+ * Load Generator options into a commander instance.
36
+ *
37
+ * @param {Generator } generator - Generator
38
+ * @return {Command } return command
39
+ */
40
+ registerGenerator ( generator : any ) {
41
+ return this . addGeneratorOptions ( generator . _options ) . addGeneratorArguments ( generator . _arguments ) ;
42
+ }
43
+
33
44
/**
34
45
* Register arguments using generator._arguments structure.
35
46
* @param {object[] } generatorArgs
36
47
* @return {YeomanCommand } this;
37
48
*/
38
- addGeneratorArguments ( generatorArgs = [ ] ) {
49
+ addGeneratorArguments ( generatorArgs : any [ ] = [ ] ) {
39
50
if ( ! generatorArgs || generatorArgs . length === 0 ) {
40
51
return this ;
41
52
}
@@ -56,7 +67,7 @@ export default class YeomanCommand extends Command {
56
67
* @param {string } blueprintOptionDescription - description of the blueprint that adds the option
57
68
* @return {YeomanCommand } this;
58
69
*/
59
- addGeneratorOptions ( options ) {
70
+ addGeneratorOptions ( options : Record < string , any > ) {
60
71
options = options || { } ;
61
72
for ( const [ key , value ] of Object . entries ( options ) ) {
62
73
this . _addGeneratorOption ( key , value ) ;
@@ -65,14 +76,14 @@ export default class YeomanCommand extends Command {
65
76
return this ;
66
77
}
67
78
68
- _addGeneratorOption ( optionName , optionDefinition , additionalDescription = '' ) {
79
+ _addGeneratorOption ( optionName : string , optionDefinition : any , additionalDescription = '' ) {
69
80
if ( optionName === 'help' ) {
70
81
return undefined ;
71
82
}
72
83
73
84
const longOption = `--${ optionName } ` ;
74
- const existingOption = this . _findOption ( longOption ) ;
75
- if ( this . _findOption ( longOption ) ) {
85
+ const existingOption = ( this as any ) . _findOption ( longOption ) ;
86
+ if ( ( this as any ) . _findOption ( longOption ) ) {
76
87
return existingOption ;
77
88
}
78
89
@@ -89,9 +100,31 @@ export default class YeomanCommand extends Command {
89
100
}
90
101
91
102
return this . addOption (
92
- new Option ( cmdString , optionDefinition . description + additionalDescription )
103
+ new Option ( cmdString , ` ${ optionDefinition . description } ${ additionalDescription } ` )
93
104
. default ( optionDefinition . default )
94
105
. hideHelp ( optionDefinition . hide ) ,
95
106
) ;
96
107
}
97
108
}
109
+
110
+ /* Add Environment options */
111
+ export const addEnvironmentOptions = ( command = new YeomanCommand ( ) ) =>
112
+ command
113
+ . option ( '--cwd' , 'Path to use as current dir' )
114
+ /* Environment options */
115
+ . option ( '--skip-install' , 'Do not automatically install dependencies' , false )
116
+ /* Generator options */
117
+ . option ( '--skip-cache' , 'Do not remember prompt answers' , false )
118
+ . option ( '--local-config-only' , 'Generate .yo-rc-global.json locally' , false )
119
+ . option ( '--ask-answered' , 'Show prompts for already configured options' , false )
120
+ /* Conflicter options */
121
+ . option ( '--force' , 'Override every file' , false )
122
+ . option ( '--dry-run' , 'Print conflicts' , false )
123
+ . option ( '--whitespace' , 'Whitespace changes will not trigger conflicts' , false )
124
+ . option ( '--bail' , 'Fail on first conflict' , false )
125
+ . option ( '--skip-yo-resolve' , 'Ignore .yo-resolve files' , false )
126
+ /* Hidden options, used for api */
127
+ . addOption ( new Option ( '--skip-local-cache' , 'Skip local answers cache' ) . default ( true ) . hideHelp ( ) )
128
+ . addOption ( new Option ( '--skip-parse-options' , 'Skip legacy options parsing' ) . default ( false ) . hideHelp ( ) )
129
+ . addOption ( new Option ( '--experimental' , 'Experimental features' ) . default ( false ) . hideHelp ( ) )
130
+ . addOption ( new Option ( '--log-cwd' , 'Path for log purpose' ) . hideHelp ( ) ) ;
0 commit comments