UNPKG

commanding

Version:

A simple yet practical command-Line application framework, written in TypeScript.

211 lines (210 loc) 8.81 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const chalk_1 = __importDefault(require("chalk")); const lodash_1 = __importDefault(require("lodash")); const cli_1 = require("../helpers/cli"); class AwesomePresenter { constructor(colorEnabled = true, output = process.stdout) { this.colorEnabled = colorEnabled; this.output = output; } write(content) { this.output.write(content); } writeWithLeftMargin(content, margin) { return this.write(cli_1.marginLeft(content, margin)); } setColor(enabled) { this.colorEnabled = enabled; } colorizeIfEnabled(str, colorizer) { return this.colorEnabled ? (colorizer ? colorizer(str) : str) : str; } getExecutableRendering(executable) { return executable; } getCommandNameRendering(name) { return this.colorizeIfEnabled(`${name}`, chalk_1.default.yellow); } getRequiredArgNameRendering(name) { return this.colorizeIfEnabled(`<${name}>`, chalk_1.default.blue); } getOptionalArgNameRendering(name) { return this.colorizeIfEnabled(`[${name}]`, chalk_1.default.greenBright); } getArgNameRendering(argRequirement) { return argRequirement.required ? this.getRequiredArgNameRendering(argRequirement.name) : this.getOptionalArgNameRendering(argRequirement.name); } getOptionNameRendering(requirement) { const strs = [ this.colorizeIfEnabled(requirement.sign, chalk_1.default.green) ]; if (requirement.name) { strs.push(this.colorizeIfEnabled(`<${requirement.name}>`, chalk_1.default.blueBright)); } return strs.join(' '); } getUsageArgRequirementsRendering(argRequirements) { return argRequirements .map(requirement => this.getArgNameRendering(requirement)) .join(' '); } getUsageExampleRendering(executable, argRequirements, command) { const prefix = command ? `${this.getExecutableRendering(executable)} ${this.getCommandNameRendering(command.getName())}` : this.getExecutableRendering(executable); return ` ${prefix} ${this.getUsageArgRequirementsRendering(argRequirements)}`; } getUsageExampleWithCommandRendering(name) { return ` ${name} ${this.getCommandNameRendering('<command>')}`; } getRequiredRendering(required) { if (required === false) { return ''; } return this.colorizeIfEnabled('required', chalk_1.default.italic); } getArgRequirementsRendering(requirements) { const table = requirements.map(requirement => ([ this.getArgNameRendering(requirement), this.colorizeIfEnabled(lodash_1.default.defaultTo(requirement.description, '')), this.getRequiredRendering(requirement.required), ])); return cli_1.alignColumns(table, AwesomePresenter.TABLE_PADDING); } getOptionRequirementsRendering(requirements) { const table = requirements.map(requirement => ([ this.getOptionNameRendering(requirement), this.colorizeIfEnabled(lodash_1.default.defaultTo(requirement.description, '')), this.getRequiredRendering(requirement.required), ])); return cli_1.alignColumns(table, AwesomePresenter.TABLE_PADDING); } getCommandRequirementsRendering(commands) { const table = commands.map(command => ([ `${this.getCommandNameRendering(command.getName())} ${this.getUsageArgRequirementsRendering(command.getArgRequirements())}`, this.colorizeIfEnabled(lodash_1.default.defaultTo(command.getDescrption(), '')), ])); return cli_1.alignColumns(table, AwesomePresenter.TABLE_PADDING); } getSectionNameRendering(name) { return this.colorizeIfEnabled(name, chalk_1.default.bold); } getApplicationNameRendering(name) { return this.colorizeIfEnabled(name, chalk_1.default.cyan); } getApplicationVersionRendering(version) { return this.colorizeIfEnabled(version); } getCommandArgumentsSectionRendering(command) { let lines = []; lines.push(this.getSectionNameRendering(`ARGUMENTS`)); lines.push(``); lines.push(cli_1.marginLeft(this.getArgRequirementsRendering(command.getArgRequirements()), 2)); lines.push(``); return lines.join(AwesomePresenter.NEW_LINE_CHAR); } getCommandOptionsSectionRendering(command) { let lines = []; lines.push(this.getSectionNameRendering(`OPTIONS`)); lines.push(``); lines.push(cli_1.marginLeft(this.getOptionRequirementsRendering(command.getOptionRequirements()), 2)); lines.push(``); return lines.join(AwesomePresenter.NEW_LINE_CHAR); } renderApplicationUsage(executable, commands, defaultCommand) { let lines = ['']; lines.push(this.getSectionNameRendering(`USAGE`)); lines.push(``); let usageShowed = false; if (defaultCommand) { lines.push(this.getUsageExampleRendering(executable, defaultCommand.getArgRequirements())); usageShowed = true; } if (commands.length > 0) { lines.push(this.getUsageExampleWithCommandRendering(executable)); usageShowed = true; } if (usageShowed === false) { lines.push(this.getUsageExampleRendering(executable, [])); } lines.push(``); if (defaultCommand) { // Arguments section. if (defaultCommand.getArgRequirements().length > 0) { lines.push(this.getCommandArgumentsSectionRendering(defaultCommand)); } // Options section. if (defaultCommand.getOptionRequirements().length > 0) { lines.push(this.getCommandOptionsSectionRendering(defaultCommand)); } } this.writeWithLeftMargin(lines.join(AwesomePresenter.NEW_LINE_CHAR), 2); } renderApplicationInfo(executable, name, description, version) { let lines = ['']; const mainName = name ? name : executable; lines.push(`${this.getApplicationNameRendering(mainName)} ${this.getApplicationVersionRendering(lodash_1.default.defaultTo(version, ''))} `); lines.push(``); if (description) { lines.push(` ${this.colorizeIfEnabled(description, chalk_1.default.gray)}`); lines.push(``); } this.writeWithLeftMargin(lines.join(AwesomePresenter.NEW_LINE_CHAR), 2); } renderCommandList(commands) { let lines = ['']; if (commands.length > 0) { lines.push(`COMMANDS`); lines.push(``); lines.push(cli_1.marginLeft(this.getCommandRequirementsRendering(commands), 2)); lines.push(``); } this.writeWithLeftMargin(lines.join(AwesomePresenter.NEW_LINE_CHAR), 2); } renderCommandHelp(executable, command) { let lines = ['']; // Usage section. lines.push(this.getSectionNameRendering(`USAGE`)); lines.push(``); lines.push(this.getUsageExampleRendering(executable, command.getArgRequirements(), command)); lines.push(``); // Arguments section. if (command.getArgRequirements().length > 0) { lines.push(this.getCommandArgumentsSectionRendering(command)); } // Options section. if (command.getOptionRequirements().length > 0) { lines.push(this.getCommandOptionsSectionRendering(command)); } this.writeWithLeftMargin(lines.join(AwesomePresenter.NEW_LINE_CHAR), 2); } renderGlobalOptions(globalOptionRequirements) { let lines = ['']; lines.push(`GLOBAL OPTIONS`); lines.push(``); lines.push(cli_1.marginLeft(this.getOptionRequirementsRendering(globalOptionRequirements), 2)); lines.push(``); this.writeWithLeftMargin(lines.join(AwesomePresenter.NEW_LINE_CHAR), 2); } renderVersion(version) { this.write(this.getApplicationVersionRendering(lodash_1.default.defaultTo(version, ''))); } renderEnding() { this.write(AwesomePresenter.NEW_LINE_CHAR); } renderError(message) { this.write(`${this.colorizeIfEnabled(message, chalk_1.default.redBright)}${AwesomePresenter.NEW_LINE_CHAR}`); } renderSplit() { this.write(AwesomePresenter.NEW_LINE_CHAR); } } exports.AwesomePresenter = AwesomePresenter; AwesomePresenter.TABLE_PADDING = 6; AwesomePresenter.NEW_LINE_CHAR = "\n";