11
22// TODO: unit tests
3-
43import type { Clerc , Command , HandlerContext , RootType , TranslateFn } from "@clerc/core" ;
54import { NoSuchCommandError , Root , definePlugin , formatCommandName , resolveCommandStrict , withBrackets } from "@clerc/core" ;
65
76import { gracefulFlagName , toArray } from "@clerc/utils" ;
87import pc from "picocolors" ;
98
10- import type { Render , Section } from "./renderer" ;
11- import { renderCliffy } from "./renderer" ;
12- import { splitTable , stringifyType } from "./utils" ;
9+ import type { Render , Renderers , Section } from "./renderer" ;
10+ import { defaultRenderers , render } from "./renderer" ;
11+ import { splitTable } from "./utils" ;
1312import { locales } from "./locales" ;
1413
14+ declare module "@clerc/core" {
15+ export interface CommandCustomProperties {
16+ help ?: Renderers
17+ }
18+ }
19+
1520const DELIMITER = pc . yellow ( "-" ) ;
1621
1722const print = ( s : string ) => { process . stdout . write ( s ) ; } ;
@@ -48,7 +53,7 @@ const generateExamples = (sections: Section[], examples: [string, string][], t:
4853 const examplesFormatted = examples . map ( ( [ command , description ] ) => [ command , DELIMITER , description ] ) ;
4954 sections . push ( {
5055 title : t ( "help.examples" ) ! ,
51- body : splitTable ( ... examplesFormatted ) ,
56+ body : splitTable ( examplesFormatted ) ,
5257 } ) ;
5358} ;
5459
@@ -76,7 +81,7 @@ const generateHelp = (render: Render, ctx: HandlerContext, notes: string[] | und
7681 } ) ;
7782 sections . push ( {
7883 title : t ( "help.commands" ) ! ,
79- body : splitTable ( ... commands ) ,
84+ body : splitTable ( commands ) ,
8085 } ) ;
8186 if ( notes ) {
8287 sections . push ( {
@@ -97,7 +102,8 @@ const generateSubcommandHelp = (render: Render, ctx: HandlerContext, command: st
97102 if ( ! subcommand ) {
98103 throw new NoSuchCommandError ( formatCommandName ( command ) , t ) ;
99104 }
100- const sections = [ ] as Section [ ] ;
105+ const renderers = Object . assign ( { } , defaultRenderers , subcommand . help ) ;
106+ let sections = [ ] as Section [ ] ;
101107 if ( command === Root ) {
102108 generateCliDetail ( sections , cli ) ;
103109 } else {
@@ -118,16 +124,17 @@ const generateSubcommandHelp = (render: Render, ctx: HandlerContext, command: st
118124 sections . push ( {
119125 title : t ( "help.flags" ) ! ,
120126 body : splitTable (
121- ...Object . entries ( subcommand . flags ) . map ( ( [ name , flag ] ) => {
122- const flagNameWithAlias = [ gracefulFlagName ( name ) ] ;
127+ Object . entries ( subcommand . flags ) . map ( ( [ name , flag ] ) => {
128+ const hasDefault = flag . default !== undefined ;
129+ let flagNameWithAlias : string [ ] = [ gracefulFlagName ( name ) ] ;
123130 if ( flag . alias ) {
124131 flagNameWithAlias . push ( gracefulFlagName ( flag . alias ) ) ;
125132 }
126- const items = [ pc . blue ( flagNameWithAlias . join ( ", " ) ) ] ;
133+ flagNameWithAlias = flagNameWithAlias . map ( renderers . renderFlagName ) ;
134+ const items = [ pc . blue ( flagNameWithAlias . join ( ", " ) ) , renderers . renderType ( flag . type , hasDefault ) ] ;
127135 items . push ( DELIMITER , flag . description || t ( "help.noDescription" ) ! ) ;
128- if ( flag . type ) {
129- const type = stringifyType ( flag . type ) ;
130- items . push ( pc . gray ( `(${ type } )` ) ) ;
136+ if ( hasDefault ) {
137+ items . push ( `(${ t ( "help.default" , renderers . renderDefault ( flag . default ) ) } )` ) ;
131138 }
132139 return items ;
133140 } ) ,
@@ -143,6 +150,7 @@ const generateSubcommandHelp = (render: Render, ctx: HandlerContext, command: st
143150 if ( subcommand . examples ) {
144151 generateExamples ( sections , subcommand . examples , t ) ;
145152 }
153+ sections = renderers . renderSections ( sections ) ;
146154 return render ( sections ) ;
147155} ;
148156
@@ -169,23 +177,17 @@ export interface HelpPluginOptions {
169177 * Banner
170178 */
171179 banner ?: string
172- /**
173- * Render type
174- */
175- renderer ?: "cliffy"
176180}
177181export const helpPlugin = ( {
178182 command = true ,
179183 showHelpWhenNoCommand = true ,
180184 notes,
181185 examples,
182186 banner,
183- renderer = "cliffy" ,
184187} : HelpPluginOptions = { } ) => definePlugin ( {
185188 setup : ( cli ) => {
186189 const { add, t } = cli . i18n ;
187190 add ( locales ) ;
188- const render : Render = renderer === "cliffy" ? renderCliffy : ( ) => "" ;
189191 const printHelp = ( s : string ) => {
190192 banner && print ( `${ banner } \n` ) ;
191193 print ( s ) ;
0 commit comments