99import yargs from 'yargs' ;
1010import { FullDescribe } from '../command-module' ;
1111
12- export interface JsonHelp {
13- name : string ;
14- shortDescription ?: string ;
15- command : string ;
16- longDescription ?: string ;
17- longDescriptionRelativePath ?: string ;
18- options : JsonHelpOption [ ] ;
19- subcommands ?: {
20- name : string ;
21- description : string ;
22- aliases : string [ ] ;
23- deprecated : string | boolean ;
24- } [ ] ;
25- }
26-
2712interface JsonHelpOption {
2813 name : string ;
2914 type ?: string ;
@@ -36,6 +21,25 @@ interface JsonHelpOption {
3621 description ?: string ;
3722}
3823
24+ interface JsonHelpDescription {
25+ shortDescription ?: string ;
26+ longDescription ?: string ;
27+ longDescriptionRelativePath ?: string ;
28+ }
29+
30+ interface JsonHelpSubcommand extends JsonHelpDescription {
31+ name : string ;
32+ aliases : string [ ] ;
33+ deprecated : string | boolean ;
34+ }
35+
36+ export interface JsonHelp extends JsonHelpDescription {
37+ name : string ;
38+ command : string ;
39+ options : JsonHelpOption [ ] ;
40+ subcommands ?: JsonHelpSubcommand [ ] ;
41+ }
42+
3943export function jsonHelpUsage ( ) : string {
4044 // eslint-disable-next-line @typescript-eslint/no-explicit-any
4145 const localYargs = yargs as any ;
@@ -102,35 +106,15 @@ export function jsonHelpUsage(): string {
102106 deprecated : string | boolean ,
103107 ] [ ]
104108 )
105- . map ( ( [ name , description , _ , aliases , deprecated ] ) => ( {
109+ . map ( ( [ name , rawDescription , _ , aliases , deprecated ] ) => ( {
106110 name : name . split ( ' ' , 1 ) [ 0 ] ,
107111 command : name ,
108- description ,
112+ ... parseDescription ( rawDescription ) ,
109113 aliases,
110114 deprecated,
111115 } ) )
112116 . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
113117
114- const parseDescription = ( rawDescription : string ) : Partial < JsonHelp > => {
115- try {
116- const {
117- longDescription,
118- describe : shortDescription ,
119- longDescriptionRelativePath,
120- } = JSON . parse ( rawDescription ) as FullDescribe ;
121-
122- return {
123- shortDescription,
124- longDescriptionRelativePath,
125- longDescription,
126- } ;
127- } catch {
128- return {
129- shortDescription : rawDescription ,
130- } ;
131- }
132- } ;
133-
134118 const [ command , rawDescription ] = usageInstance . getUsage ( ) [ 0 ] ?? [ ] ;
135119
136120 const output : JsonHelp = {
@@ -143,3 +127,23 @@ export function jsonHelpUsage(): string {
143127
144128 return JSON . stringify ( output , undefined , 2 ) ;
145129}
130+
131+ function parseDescription ( rawDescription : string ) : JsonHelpDescription {
132+ try {
133+ const {
134+ longDescription,
135+ describe : shortDescription ,
136+ longDescriptionRelativePath,
137+ } = JSON . parse ( rawDescription ) as FullDescribe ;
138+
139+ return {
140+ shortDescription,
141+ longDescriptionRelativePath,
142+ longDescription,
143+ } ;
144+ } catch {
145+ return {
146+ shortDescription : rawDescription ,
147+ } ;
148+ }
149+ }
0 commit comments