9
9
import yargs from 'yargs' ;
10
10
import { FullDescribe } from '../command-module' ;
11
11
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
-
27
12
interface JsonHelpOption {
28
13
name : string ;
29
14
type ?: string ;
@@ -36,6 +21,25 @@ interface JsonHelpOption {
36
21
description ?: string ;
37
22
}
38
23
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
+
39
43
export function jsonHelpUsage ( ) : string {
40
44
// eslint-disable-next-line @typescript-eslint/no-explicit-any
41
45
const localYargs = yargs as any ;
@@ -102,35 +106,15 @@ export function jsonHelpUsage(): string {
102
106
deprecated : string | boolean ,
103
107
] [ ]
104
108
)
105
- . map ( ( [ name , description , _ , aliases , deprecated ] ) => ( {
109
+ . map ( ( [ name , rawDescription , _ , aliases , deprecated ] ) => ( {
106
110
name : name . split ( ' ' , 1 ) [ 0 ] ,
107
111
command : name ,
108
- description ,
112
+ ... parseDescription ( rawDescription ) ,
109
113
aliases,
110
114
deprecated,
111
115
} ) )
112
116
. sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
113
117
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
-
134
118
const [ command , rawDescription ] = usageInstance . getUsage ( ) [ 0 ] ?? [ ] ;
135
119
136
120
const output : JsonHelp = {
@@ -143,3 +127,23 @@ export function jsonHelpUsage(): string {
143
127
144
128
return JSON . stringify ( output , undefined , 2 ) ;
145
129
}
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