6
6
*/
7
7
8
8
import { join } from 'node:path' ;
9
- import { asString , Dictionary , ensureObject , ensureString } from '@salesforce/ts-types' ;
9
+ import { asString , Dictionary , ensureObject , ensureString , Optional } from '@salesforce/ts-types' ;
10
10
import { CommandClass , CommandData , CommandParameterData , punctuate , replaceConfigVariables } from '../utils.js' ;
11
11
import { Ditamap } from './ditamap.js' ;
12
12
@@ -21,14 +21,18 @@ type FlagInfo = {
21
21
default : string | ( ( ) => Promise < string > ) ;
22
22
} ;
23
23
24
- const getDefault = async ( flag ? : FlagInfo ) : Promise < string > => {
24
+ const getDefault = async ( flag : FlagInfo , flagName : string ) : Promise < string > => {
25
25
if ( ! flag ) {
26
26
return '' ;
27
27
}
28
+ if ( flagName === 'target-org' || flagName === 'target-dev-hub' ) {
29
+ // special handling to prevent global/local default usernames from appearing in the docs, but they do appear in user's help
30
+ return '' ;
31
+ }
28
32
if ( typeof flag . default === 'function' ) {
29
33
try {
30
34
const help = await flag . default ( ) ;
31
- return help || '' ;
35
+ return help . includes ( '[object Object]' ) ? '' : help ?? '' ;
32
36
} catch {
33
37
return '' ;
34
38
}
@@ -50,23 +54,23 @@ export class Command extends Ditamap {
50
54
) {
51
55
const commandWithUnderscores = ensureString ( command . id ) . replace ( / : / g, '_' ) ;
52
56
const filename = Ditamap . file ( `cli_reference_${ commandWithUnderscores } ` , 'xml' ) ;
53
-
54
57
super ( filename , undefined ) ;
55
58
56
59
this . flags = ensureObject ( command . flags ) ;
57
60
this . commandMeta = commandMeta ;
61
+ const binary = readBinary ( this . commandMeta ) ;
58
62
59
63
const summary = punctuate ( command . summary ) ;
60
64
this . commandName = command . id . replace ( / : / g, asString ( this . commandMeta . topicSeparator , ':' ) ) ;
61
65
62
66
const description = command . description
63
- ? replaceConfigVariables ( command . description , asString ( this . commandMeta . binary , 'unknown' ) , this . commandName )
67
+ ? replaceConfigVariables ( command . description , binary , this . commandName )
64
68
: undefined ;
65
69
66
70
// Help are all the lines after the first line in the description. Before oclif, there was a 'help' property so continue to
67
71
// support that.
68
72
69
- const help = this . formatParagraphs ( description ) ;
73
+ const help = formatParagraphs ( description ) ;
70
74
71
75
let trailblazerCommunityUrl : string | undefined ;
72
76
let trailblazerCommunityName : string | undefined ;
@@ -90,10 +94,8 @@ export class Command extends Ditamap {
90
94
}
91
95
92
96
return {
93
- description : replaceConfigVariables ( desc ?? '' , asString ( this . commandMeta . binary , 'unknown' ) , this . commandName ) ,
94
- commands : commands . map ( ( cmd ) =>
95
- replaceConfigVariables ( cmd , asString ( this . commandMeta . binary , 'unknown' ) , this . commandName )
96
- ) ,
97
+ description : replaceConfigVariables ( desc ?? '' , binary , this . commandName ) ,
98
+ commands : commands . map ( ( cmd ) => replaceConfigVariables ( cmd , binary , this . commandName ) ) ,
97
99
} ;
98
100
} ) ;
99
101
@@ -102,7 +104,7 @@ export class Command extends Ditamap {
102
104
name : this . commandName ,
103
105
summary,
104
106
description,
105
- binary : 'binary' in commandMeta && typeof commandMeta . binary === 'string' ? commandMeta . binary : 'unknown' ,
107
+ binary,
106
108
commandWithUnderscores,
107
109
deprecated : ( command . deprecated as boolean ) ?? state === 'deprecated' ?? false ,
108
110
examples,
@@ -120,34 +122,24 @@ export class Command extends Ditamap {
120
122
}
121
123
122
124
public async getParametersForTemplate ( flags : Dictionary < FlagInfo > ) : Promise < CommandParameterData [ ] > {
123
- const final : CommandParameterData [ ] = [ ] ;
124
-
125
- for ( const [ flagName , flag ] of Object . entries ( flags ) ) {
126
- if ( ! flag || flag . hidden ) continue ;
127
- const description = replaceConfigVariables (
128
- Array . isArray ( flag ?. description ) ? flag ?. description . join ( '\n' ) : flag ?. description ?? '' ,
129
- asString ( this . commandMeta . binary , 'unknown' ) ,
130
- this . commandName
131
- ) ;
132
- const entireDescription = flag . summary
133
- ? `${ replaceConfigVariables (
134
- flag . summary ,
135
- asString ( this . commandMeta . binary , 'unknown' ) ,
136
- this . commandName
137
- ) } \n${ description } `
138
- : description ;
139
- const updated = Object . assign ( { } , flag , {
140
- name : flagName ,
141
- description : this . formatParagraphs ( entireDescription ) ,
142
- optional : ! flag ?. required ,
143
- kind : flag ?. kind ?? flag ?. type ,
144
- hasValue : flag ?. type !== 'boolean' ,
145
- // eslint-disable-next-line no-await-in-loop
146
- defaultFlagValue : await getDefault ( flag ) ,
147
- } ) ;
148
- final . push ( updated ) ;
149
- }
150
- return final ;
125
+ const descriptionBuilder = buildDescription ( this . commandName ) ( readBinary ( this . commandMeta ) ) ;
126
+ return Promise . all (
127
+ [ ...Object . entries ( flags ) ]
128
+ . filter ( flagIsDefined )
129
+ . filter ( ( [ , flag ] ) => ! flag . hidden )
130
+ . map (
131
+ async ( [ flagName , flag ] ) =>
132
+ ( {
133
+ ...flag ,
134
+ name : flagName ,
135
+ description : descriptionBuilder ( flag ) ,
136
+ optional : ! flag . required ,
137
+ kind : flag . kind ?? flag . type ,
138
+ hasValue : flag . type !== 'boolean' ,
139
+ defaultFlagValue : await getDefault ( flag , flagName ) ,
140
+ } satisfies CommandParameterData )
141
+ )
142
+ ) ;
151
143
}
152
144
153
145
// eslint-disable-next-line class-methods-use-this
@@ -161,3 +153,25 @@ export class Command extends Ditamap {
161
153
return super . transformToDitamap ( ) ;
162
154
}
163
155
}
156
+
157
+ const flagIsDefined = ( input : [ string , Optional < FlagInfo > ] ) : input is [ string , FlagInfo ] => input [ 1 ] !== undefined ;
158
+
159
+ const buildDescription =
160
+ ( commandName : string ) =>
161
+ ( binary : string ) =>
162
+ ( flag : FlagInfo ) : string [ ] => {
163
+ const description = replaceConfigVariables (
164
+ Array . isArray ( flag ?. description ) ? flag ?. description . join ( '\n' ) : flag ?. description ?? '' ,
165
+ binary ,
166
+ commandName
167
+ ) ;
168
+ return formatParagraphs (
169
+ flag . summary ? `${ replaceConfigVariables ( flag . summary , binary , commandName ) } \n${ description } ` : description
170
+ ) ;
171
+ } ;
172
+
173
+ const formatParagraphs = ( textToFormat ?: string ) : string [ ] =>
174
+ textToFormat ? textToFormat . split ( '\n' ) . filter ( ( n ) => n !== '' ) : [ ] ;
175
+
176
+ const readBinary = ( commandMeta : Record < string , unknown > ) : string =>
177
+ 'binary' in commandMeta && typeof commandMeta . binary === 'string' ? commandMeta . binary : 'unknown' ;
0 commit comments