-
Notifications
You must be signed in to change notification settings - Fork 642
/
help.ts
107 lines (84 loc) · 4.67 KB
/
help.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { CommandHelpFormatterDeps as BaseCommandHelpFormatterDeps, CommandHelpSchema as BaseCommandHelpSchema, CommandSchemaHelpFormatter as BaseCommandSchemaHelpFormatter, CommandStringHelpFormatter as BaseCommandStringHelpFormatter, MetadataGroup, NO_COLORS, NamespaceHelpFormatterDeps as BaseNamespaceHelpFormatterDeps, NamespaceSchemaHelpFormatter as BaseNamespaceSchemaHelpFormatter, NamespaceStringHelpFormatter as BaseNamespaceStringHelpFormatter, formatOptionName, isOptionVisible } from '@ionic/cli-framework';
import { filter } from '@ionic/utils-array';
import { CommandMetadata, CommandMetadataInput, CommandMetadataOption, HydratedCommandMetadata, ICommand, INamespace } from '../definitions';
import { COLORS } from './color';
import { GLOBAL_OPTIONS } from './config';
const IONIC_LOGO = String.raw`
_ _
(_) ___ _ __ (_) ___
| |/ _ \| '_ \| |/ __|
| | (_) | | | | | (__
|_|\___/|_| |_|_|\___|`;
export interface NamespaceHelpFormatterDeps extends BaseNamespaceHelpFormatterDeps<ICommand, INamespace, CommandMetadata, CommandMetadataInput, CommandMetadataOption> {
readonly inProject: boolean;
readonly version: string;
}
export interface CommandHelpFormatterDeps extends BaseCommandHelpFormatterDeps<ICommand, INamespace, CommandMetadata, CommandMetadataInput, CommandMetadataOption> {}
export class NamespaceStringHelpFormatter extends BaseNamespaceStringHelpFormatter<ICommand, INamespace, CommandMetadata, CommandMetadataInput, CommandMetadataOption> {
protected readonly inProject: boolean;
protected readonly version: string;
constructor({ version, inProject, ...rest }: NamespaceHelpFormatterDeps) {
super({ ...rest, colors: COLORS });
this.inProject = inProject;
this.version = version;
}
async formatHeader(): Promise<string> {
return this.namespace.parent ? super.formatHeader() : this.formatIonicHeader();
}
async formatIonicHeader(): Promise<string> {
const { strong } = this.colors;
return `\n${IONIC_LOGO} ${strong(`CLI ${this.version}`)}\n\n`;
}
async getGlobalOptions(): Promise<string[]> {
const visibleOptions = await filter(GLOBAL_OPTIONS, async opt => isOptionVisible(opt));
return visibleOptions.map(opt => formatOptionName(opt, { colors: NO_COLORS, showAliases: false }));
}
async formatCommands() {
const { strong } = this.colors;
const commands = await this.getCommandMetadataList();
const globalCmds = commands.filter(cmd => cmd.type === 'global');
const projectCmds = commands.filter(cmd => cmd.type === 'project');
return (
(await this.formatCommandGroup('Global Commands', globalCmds)) +
(this.inProject ? await this.formatCommandGroup('Project Commands', projectCmds) : `\n ${strong('Project Commands')}:\n\n You are not in a project directory.\n`)
);
}
}
export class CommandStringHelpFormatter extends BaseCommandStringHelpFormatter<ICommand, INamespace, CommandMetadata, CommandMetadataInput, CommandMetadataOption> {
constructor(options: CommandHelpFormatterDeps) {
super({ ...options, colors: COLORS });
}
async formatOptions(): Promise<string> {
const metadata = await this.getCommandMetadata();
const options = metadata.options ? metadata.options : [];
const basicOptions = options.filter(o => !o.groups || !o.groups.includes(MetadataGroup.ADVANCED));
const advancedOptions = options.filter(o => o.groups && o.groups.includes(MetadataGroup.ADVANCED));
return (
(await this.formatOptionsGroup('Options', basicOptions)) +
(await this.formatOptionsGroup('Advanced Options', advancedOptions))
);
}
async formatBeforeOptionSummary(opt: CommandMetadataOption): Promise<string> {
return (opt.hint ? `${opt.hint} ` : '') + await super.formatBeforeOptionSummary(opt);
}
}
export class NamespaceSchemaHelpFormatter extends BaseNamespaceSchemaHelpFormatter<ICommand, INamespace, CommandMetadata, CommandMetadataInput, CommandMetadataOption> {
async formatCommand(cmd: HydratedCommandMetadata): Promise<CommandHelpSchema> {
const { command } = cmd;
const formatter = new CommandSchemaHelpFormatter({
location: { path: [...cmd.path], obj: command, args: [] },
command,
metadata: cmd,
});
return { ...await formatter.serialize(), type: cmd.type };
}
}
export interface CommandHelpSchema extends BaseCommandHelpSchema {
type: string;
}
export class CommandSchemaHelpFormatter extends BaseCommandSchemaHelpFormatter<ICommand, INamespace, CommandMetadata, CommandMetadataInput, CommandMetadataOption> {
async formatCommand(cmd: CommandMetadata | HydratedCommandMetadata): Promise<CommandHelpSchema> {
const formatted = await super.formatCommand(cmd);
return { ...formatted, type: cmd.type };
}
}