Skip to content

Commit

Permalink
feat(help): formatBeforeInputSummary and formatAfterInputSummary
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd committed Jun 1, 2020
1 parent 9993aa5 commit 70ac3ab
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
1 change: 0 additions & 1 deletion packages/@ionic/cli-framework/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export interface CommandMetadataInput {
name: string;
summary: string;
validators?: Validator[];
private?: boolean;
}

export interface TextFootnote {
Expand Down
4 changes: 2 additions & 2 deletions packages/@ionic/cli-framework/src/lib/__tests__/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ describe('@ionic/cli-framework', () => {
Inputs:
input1 .......................... input1 summary
input2 .......................... input2 summary
input1 .......................... (optional) input1 summary
input2 .......................... (optional) input2 summary
Options:
Expand Down
59 changes: 47 additions & 12 deletions packages/@ionic/cli-framework/src/lib/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,27 +492,36 @@ export class CommandStringHelpFormatter<C extends ICommand<C, N, M, I, O>, N ext
}

async formatInputs(): Promise<string> {
const { help: { title }, weak, input } = this.colors;
const { help: { title } } = this.colors;
const metadata = await this.getCommandMetadata();
const inputs = metadata.inputs ? metadata.inputs : [];

if (inputs.length === 0) {
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<string> {
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) {
Expand All @@ -534,7 +543,7 @@ export class CommandStringHelpFormatter<C extends ICommand<C, N, M, I, O>, 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.
*/
Expand All @@ -543,14 +552,35 @@ export class CommandStringHelpFormatter<C extends ICommand<C, N, M, I, O>, 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.
*/
async formatAfterSummary(meta: M): Promise<string> {
return '';
}

/**
* Insert text that appears before the input's summary.
*
* @param input The metadata of the input.
*/
async formatBeforeInputSummary(input: I): Promise<string> {
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<string> {
return '';
}

/**
* Insert text that appears before an option's summary.
*
Expand All @@ -560,6 +590,11 @@ export class CommandStringHelpFormatter<C extends ICommand<C, N, M, I, O>, 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<string> {
return this.formatOptionDefault(opt);
}
Expand Down

0 comments on commit 70ac3ab

Please sign in to comment.