From 70ac3ab633dc7dd65a2ac344c81a01f7914597b8 Mon Sep 17 00:00:00 2001 From: Daniel Imhoff Date: Mon, 1 Jun 2020 12:19:20 -0700 Subject: [PATCH] feat(help): formatBeforeInputSummary and formatAfterInputSummary --- .../@ionic/cli-framework/src/definitions.ts | 1 - .../cli-framework/src/lib/__tests__/help.ts | 4 +- packages/@ionic/cli-framework/src/lib/help.ts | 59 +++++++++++++++---- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/packages/@ionic/cli-framework/src/definitions.ts b/packages/@ionic/cli-framework/src/definitions.ts index 42c29b7d3f..405dba8576 100644 --- a/packages/@ionic/cli-framework/src/definitions.ts +++ b/packages/@ionic/cli-framework/src/definitions.ts @@ -15,7 +15,6 @@ export interface CommandMetadataInput { name: string; summary: string; validators?: Validator[]; - private?: boolean; } export interface TextFootnote { diff --git a/packages/@ionic/cli-framework/src/lib/__tests__/help.ts b/packages/@ionic/cli-framework/src/lib/__tests__/help.ts index 9ecb02018c..f367d6d6fa 100644 --- a/packages/@ionic/cli-framework/src/lib/__tests__/help.ts +++ b/packages/@ionic/cli-framework/src/lib/__tests__/help.ts @@ -147,8 +147,8 @@ describe('@ionic/cli-framework', () => { Inputs: - input1 .......................... input1 summary - input2 .......................... input2 summary + input1 .......................... (optional) input1 summary + input2 .......................... (optional) input2 summary Options: diff --git a/packages/@ionic/cli-framework/src/lib/help.ts b/packages/@ionic/cli-framework/src/lib/help.ts index 0ee16e20ef..eb1f97b795 100644 --- a/packages/@ionic/cli-framework/src/lib/help.ts +++ b/packages/@ionic/cli-framework/src/lib/help.ts @@ -492,7 +492,7 @@ export class CommandStringHelpFormatter, N ext } async formatInputs(): Promise { - const { help: { title }, weak, input } = this.colors; + const { help: { title } } = this.colors; const metadata = await this.getCommandMetadata(); const inputs = metadata.inputs ? metadata.inputs : []; @@ -500,19 +500,28 @@ export class CommandStringHelpFormatter, N ext return ''; } - const fillStrings = generateFillSpaceStringList(inputs.map(i => i.name), this.dotswidth, weak('.')); - - const inputLineFn = ({ name, summary }: I, index: number) => { - const optionList = input(`${name}`); - const wrappedSummary = wordWrap(summary, { indentation: this.dotswidth + 6 }); - - return `${optionList} ${fillStrings[index]} ${wrappedSummary}`; - }; + const formattedInputs = await Promise.all(inputs.map(input => this.formatInput(input))); return ( `\n ${title('Inputs')}:` + - `\n\n ${inputs.map(inputLineFn).join('\n ')}\n` + `\n\n ${formattedInputs.join('\n ')}\n` + ); + } + + async formatInput(i: I): Promise { + const { input, weak } = this.colors; + const inputName = input(i.name); + const inputNameLength = stringWidth(inputName); + const fullLength = inputNameLength > this.dotswidth ? inputNameLength + 1 : this.dotswidth; + const fullDescription = ( + (await this.formatBeforeInputSummary(i)) + + i.summary + + (await this.formatAfterInputSummary(i)) ); + + const wrappedDescription = wordWrap(fullDescription, { indentation: this.dotswidth + 6 }); + + return `${inputName} ${weak('.').repeat(fullLength - inputNameLength)} ${wrappedDescription}`; } async formatOptionLine(opt: O) { @@ -534,7 +543,7 @@ export class CommandStringHelpFormatter, N ext } /** - * Insert text before the command's summary. + * Insert text that appears before the command's summary. * * @param meta The metadata of the command. */ @@ -543,7 +552,7 @@ export class CommandStringHelpFormatter, N ext } /** - * Insert text after the command's summary. + * Insert text that appears after the command's summary. * * @param meta The metadata of the command. */ @@ -551,6 +560,27 @@ export class CommandStringHelpFormatter, N ext return ''; } + /** + * Insert text that appears before the input's summary. + * + * @param input The metadata of the input. + */ + async formatBeforeInputSummary(input: I): Promise { + const { weak } = this.colors; + const required = input.validators && input.validators.includes(validators.required) ? true : false; + + return required ? '' : `${weak('(optional)')} `; + } + + /** + * Insert text that appears after the input's summary. + * + * @param input The metadata of the input. + */ + async formatAfterInputSummary(input: I): Promise { + return ''; + } + /** * Insert text that appears before an option's summary. * @@ -560,6 +590,11 @@ export class CommandStringHelpFormatter, N ext return formatHelpGroups(opt.groups, this.colors); } + /** + * Insert text that appears after the option's summary. + * + * @param opt The metadata of the option. + */ async formatAfterOptionSummary(opt: O): Promise { return this.formatOptionDefault(opt); }