@ionic/cli-utils
Version:
Ionic CLI Utils
167 lines (166 loc) • 7.52 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const cli_framework_1 = require("@ionic/cli-framework");
const chalk_1 = require("chalk");
const config_1 = require("./config");
const IONIC_LOGO = String.raw `
_ _
(_) ___ _ __ (_) ___
| |/ _ \| '_ \| |/ __|
| | (_) | | | | | (__
|_|\___/|_| |_|_|\___|`;
const OPTION_DECORATIONS = [
[cli_framework_1.OptionGroup.Beta, chalk_1.default.red.bold('(beta)')],
[cli_framework_1.OptionGroup.Deprecated, chalk_1.default.yellow.bold('(deprecated)')],
[cli_framework_1.OptionGroup.Experimental, chalk_1.default.red.bold('(experimental)')],
];
const COMMAND_DECORATIONS = [
[cli_framework_1.CommandGroup.Beta, chalk_1.default.red.bold('(beta)')],
[cli_framework_1.CommandGroup.Deprecated, chalk_1.default.yellow.bold('(deprecated)')],
[cli_framework_1.CommandGroup.Experimental, chalk_1.default.red.bold('(experimental)')],
];
const NAMESPACE_DECORATIONS = [
[cli_framework_1.NamespaceGroup.Beta, chalk_1.default.red.bold('(beta)')],
[cli_framework_1.NamespaceGroup.Deprecated, chalk_1.default.yellow.bold('(deprecated)')],
[cli_framework_1.NamespaceGroup.Experimental, chalk_1.default.red.bold('(experimental)')],
];
function isCommandVisible(cmd) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const ns = yield cmd.namespace.getMetadata();
return (!cmd.groups || !cmd.groups.includes(cli_framework_1.CommandGroup.Hidden)) && (!ns.groups || !ns.groups.includes(cli_framework_1.NamespaceGroup.Hidden));
});
}
exports.isCommandVisible = isCommandVisible;
function isOptionVisible(opt) {
return !opt.groups || !opt.groups.includes(cli_framework_1.OptionGroup.Hidden);
}
exports.isOptionVisible = isOptionVisible;
class NamespaceStringHelpFormatter extends cli_framework_1.NamespaceStringHelpFormatter {
constructor(_a) {
var { version, inProject } = _a, rest = tslib_1.__rest(_a, ["version", "inProject"]);
super(rest);
this.inProject = inProject;
this.version = version;
}
formatHeader() {
const _super = name => super[name];
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this.namespace.parent ? _super("formatHeader").call(this) : this.formatIonicHeader();
});
}
formatIonicHeader() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return IONIC_LOGO + ` CLI ${this.version}\n\n`;
});
}
formatBeforeNamespaceSummary(meta) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return formatGroupDecorations(NAMESPACE_DECORATIONS, meta.groups);
});
}
formatBeforeSummary() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const metadata = yield this.getNamespaceMetadata();
return formatGroupDecorations(NAMESPACE_DECORATIONS, metadata.groups);
});
}
formatBeforeCommandSummary(cmd) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return formatGroupDecorations(COMMAND_DECORATIONS, cmd.groups);
});
}
getExtraOptions() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return config_1.GLOBAL_OPTIONS
.filter(opt => isOptionVisible(opt))
.map(opt => cli_framework_1.formatOptionName(opt, { showAliases: false }));
});
}
formatCommands() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const { strong } = this.colors;
const commands = yield this.namespace.getCommandMetadataList();
const globalCmds = commands.filter(cmd => cmd.type === 'global');
const projectCmds = commands.filter(cmd => cmd.type === 'project');
return ((yield this.formatCommandGroup('Global Commands', globalCmds)) +
(this.inProject ? yield this.formatCommandGroup('Project Commands', projectCmds) : `\n ${strong('Project Commands')}:\n\n You are not in a project directory.\n`));
});
}
filterCommandCallback(cmd) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return isCommandVisible(cmd);
});
}
}
exports.NamespaceStringHelpFormatter = NamespaceStringHelpFormatter;
class CommandStringHelpFormatter extends cli_framework_1.CommandStringHelpFormatter {
formatOptions() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const metadata = yield this.getCommandMetadata();
const options = metadata.options ? metadata.options : [];
const basicOptions = options.filter(o => !o.groups || !o.groups.includes(cli_framework_1.OptionGroup.Advanced));
const advancedOptions = options.filter(o => o.groups && o.groups.includes(cli_framework_1.OptionGroup.Advanced));
return ((yield this.formatOptionsGroup('Options', basicOptions)) +
(yield this.formatOptionsGroup('Advanced Options', advancedOptions)));
});
}
filterOptionCallback(opt) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return isOptionVisible(opt);
});
}
formatBeforeSummary() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const metadata = yield this.getCommandMetadata();
return formatGroupDecorations(COMMAND_DECORATIONS, metadata.groups);
});
}
formatBeforeOptionSummary(opt) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return (opt.hint ? `${opt.hint} ` : '') + formatGroupDecorations(OPTION_DECORATIONS, opt.groups);
});
}
}
exports.CommandStringHelpFormatter = CommandStringHelpFormatter;
class NamespaceSchemaHelpFormatter extends cli_framework_1.NamespaceSchemaHelpFormatter {
filterCommandCallback(cmd) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return isCommandVisible(cmd);
});
}
formatCommand(cmd) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const { command } = cmd;
const formatter = new CommandSchemaHelpFormatter({
location: { path: [...cmd.path], obj: command, args: [] },
command,
metadata: cmd,
});
return Object.assign({}, yield formatter.serialize(), { type: cmd.type });
});
}
}
exports.NamespaceSchemaHelpFormatter = NamespaceSchemaHelpFormatter;
class CommandSchemaHelpFormatter extends cli_framework_1.CommandSchemaHelpFormatter {
formatCommand(cmd) {
const _super = name => super[name];
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const formatted = yield _super("formatCommand").call(this, cmd);
return Object.assign({}, formatted, { type: cmd.type });
});
}
filterOptionCallback(opt) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return isOptionVisible(opt);
});
}
}
exports.CommandSchemaHelpFormatter = CommandSchemaHelpFormatter;
function formatGroupDecorations(decorations, groups) {
if (!groups) {
return '';
}
const prepends = decorations.filter(([g]) => groups.includes(g)).map(([, d]) => d);
return prepends.length ? prepends.join(' ') + ' ' : '';
}
;