appcenter-cli
Version:
Command line tool for Visual Studio App Center
109 lines (108 loc) • 5.08 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CategoryCommand = void 0;
const fs = require("fs");
const path = require("path");
const command_1 = require("./command");
const command_result_1 = require("./command-result");
const interaction_1 = require("../interaction");
const misc_1 = require("../misc");
const option_decorators_1 = require("./option-decorators");
const chalk = require("chalk");
const Table = require("cli-table3");
const debug = require("debug")("appcenter-cli:util:commandline:category-command");
// "filler" command used to display category help
class CategoryCommand extends command_1.Command {
constructor(args) {
super(args);
}
execute() {
return __awaiter(this, void 0, void 0, function* () {
if (this.version) {
debug("Version switch detected, displaying version number");
return this.showVersion();
}
interaction_1.out.help();
interaction_1.out.help(this.categoryHelp());
interaction_1.out.help(`Version ${this.getVersion()}`);
interaction_1.out.help();
const command = "<command>";
const commandTemplate = [misc_1.scriptName].concat(this.command, [command]).join(" ");
interaction_1.out.help(`Usage: ${chalk.bold(commandTemplate)}`);
interaction_1.out.help();
interaction_1.out.help("Commands:");
const categoryContents = this.categoryDirContents();
const subCategoriesHelp = this.subCategories(categoryContents);
const categoryCommands = this.categoryCommands(categoryContents);
const helpTable = subCategoriesHelp.concat(categoryCommands);
// Calculate max length of the strings from the first column (category/commands names) - it will be a width for the first column;
const firstColumnWidth = helpTable.reduce((contenderMaxWidth, row) => Math.max(row[0].length, contenderMaxWidth), 0);
// Writing a help table
const tableObject = new Table(interaction_1.out.getOptionsForTwoColumnTableWithNoBorders(firstColumnWidth));
helpTable.forEach((row) => tableObject.push(row));
interaction_1.out.help(tableObject.toString());
return command_result_1.success();
});
}
categoryHelp(category = "") {
debug(`Looking for category description in directory ${this.commandPath}`);
const helpPath = path.join(this.commandPath, category, "category.txt");
try {
// Replacing CRLF with LF to make sure that cli-table3 will be able to correctly split the string
const helpText = fs.readFileSync(helpPath, "utf8").replace(/\r\n/g, "\n");
return helpText;
}
catch (err) {
if (err.code === "ENOENT") {
return "No category description found";
}
throw err;
}
}
categoryDirContents() {
const dirFiles = fs.readdirSync(this.commandPath);
return dirFiles.map((fileName) => {
return [fileName, fs.statSync(path.join(this.commandPath, fileName))];
});
}
subCategories(contents) {
return contents
.filter((item) => item[1].isDirectory() && item[0] !== "lib")
.map((item) => {
return [chalk.bold(` ${item[0]} `), this.categoryHelp(item[0])];
});
}
categoryCommands(contents) {
// Locate commands in category directory
return contents
.filter((item) => item[1].isFile() && /\.[tj]s$/.test(item[0]))
.map((item) => {
return [chalk.bold(` ${this.commandName(item)} `), this.commandHelp(item)];
});
}
commandName(item) {
return path.parse(item[0]).name;
}
commandHelp(item) {
const fullCommandPath = path.join(this.commandPath, item[0]);
try {
// Turn off lint error, string is validated above
// eslint-disable-next-line security/detect-non-literal-require
const cmd = require(fullCommandPath).default;
return option_decorators_1.getClassHelpText(cmd);
}
catch (err) {
return `Unable to load ${fullCommandPath} to read help`;
}
}
}
exports.CategoryCommand = CategoryCommand;