@@ -25,17 +25,35 @@ export type CommandHelpInfo = {
25
25
required : boolean ;
26
26
kind : string ;
27
27
type : string ;
28
+ defaultHelpValue ?: string ;
29
+ default : string | ( ( ) => Promise < string > ) ;
30
+ } ;
31
+
32
+ const getDefault = async ( flag : CommandHelpInfo ) : Promise < string > => {
33
+ if ( typeof flag . default !== 'function' ) {
34
+ return flag . default ;
35
+ } else if ( typeof flag . default === 'function' ) {
36
+ try {
37
+ const help = await flag . default ( ) ;
38
+ return help || '' ;
39
+ } catch {
40
+ return '' ;
41
+ }
42
+ } else {
43
+ return '' ;
44
+ }
28
45
} ;
29
46
30
47
export class Command extends Ditamap {
48
+ private flags : Dictionary < CommandHelpInfo > ;
49
+
31
50
public constructor ( topic : string , subtopic : string , command : Dictionary , commandMeta : JsonMap = { } ) {
32
51
const commandWithUnderscores = ensureString ( command . id ) . replace ( / : / g, '_' ) ;
33
52
const filename = Ditamap . file ( `cli_reference_${ commandWithUnderscores } ` , 'xml' ) ;
34
53
35
54
super ( filename , { } ) ;
36
55
37
- const flags = ensureObject ( command . flags ) ;
38
- const parameters = this . getParametersForTemplate ( flags as Dictionary < CommandHelpInfo > ) ;
56
+ this . flags = ensureObject ( command . flags ) ;
39
57
40
58
const summary = punctuate ( asString ( command . summary ) ) ;
41
59
@@ -86,7 +104,6 @@ export class Command extends Ditamap {
86
104
summary,
87
105
description,
88
106
help,
89
- parameters,
90
107
isClosedPilotCommand : state === 'closedPilot' ,
91
108
isOpenPilotCommand : state === 'openPilot' ,
92
109
isBetaCommand : state === 'beta' ,
@@ -97,24 +114,35 @@ export class Command extends Ditamap {
97
114
this . destination = join ( Ditamap . outputDir , topic , filename ) ;
98
115
}
99
116
100
- public getParametersForTemplate ( flags : Dictionary < CommandHelpInfo > ) {
101
- return Object . entries ( flags )
102
- . filter ( ( [ , flag ] ) => ! flag . hidden )
103
- . map ( ( [ flagName , flag ] ) => {
104
- const description = Array . isArray ( flag . description ) ? flag . description . join ( '\n' ) : flag . description || '' ;
105
- const entireDescription = flag . summary ? ` ${ flag . summary } \n ${ description } ` : description ;
106
-
107
- return Object . assign ( flag , {
108
- name : flagName ,
109
- description : this . formatParagraphs ( entireDescription ) ,
110
- optional : ! flag . required ,
111
- kind : flag . kind || flag . type ,
112
- hasValue : flag . type !== 'boolean' ,
113
- } ) ;
117
+ public async getParametersForTemplate ( flags : Dictionary < CommandHelpInfo > ) {
118
+ const final = [ ] as CommandHelpInfo [ ] ;
119
+
120
+ for ( const [ flagName , flag ] of Object . entries ( flags ) ) {
121
+ if ( flag . hidden ) continue ;
122
+ const description = Array . isArray ( flag . description ) ? flag . description . join ( '\n' ) : flag . description || '' ;
123
+ const entireDescription = flag . summary ? ` ${ flag . summary } \n ${ description } ` : description ;
124
+ const updated = Object . assign ( flag , {
125
+ name : flagName ,
126
+ description : this . formatParagraphs ( entireDescription ) ,
127
+ optional : ! flag . required ,
128
+ kind : flag . kind || flag . type ,
129
+ hasValue : flag . type !== 'boolean' ,
130
+ defaultFlagValue : await getDefault ( flag ) ,
114
131
} ) ;
132
+ final . push ( updated ) ;
133
+ }
134
+ return final ;
115
135
}
116
136
117
137
public getTemplateFileName ( ) : string {
118
138
return 'command.hbs' ;
119
139
}
140
+
141
+ protected async transformToDitamap ( ) {
142
+ // eslint-disable-next-line no-console
143
+ console . log ( 'transformToDitamap' ) ;
144
+ const parameters = await this . getParametersForTemplate ( this . flags ) ;
145
+ this . data = Object . assign ( { } , this . data , { parameters } ) ;
146
+ return super . transformToDitamap ( ) ;
147
+ }
120
148
}
0 commit comments