UNPKG

appcenter-cli

Version:

Command line tool for Visual Studio App Center

193 lines (192 loc) 9.29 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; 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.Command = void 0; // Base class for all command handlers const Result = require("./command-result"); const option_decorators_1 = require("./option-decorators"); const option_parser_1 = require("./option-parser"); const interaction_1 = require("../interaction"); const help_1 = require("./help"); const misc_1 = require("../misc"); const profile_1 = require("../profile"); const apis_1 = require("../apis"); const path = require("path"); const debug = require("debug")("appcenter-cli:util:commandline:command"); class Command { constructor(args) { // Additional output formats (except "list" which is used by default) which are supported by this command this.additionalSupportedOutputFormats = { json: interaction_1.setFormatJson, }; const proto = Object.getPrototypeOf(this); const flags = option_decorators_1.getOptionsDescription(proto); const positionals = option_decorators_1.getPositionalOptionsDescription(proto); option_parser_1.parseOptions(flags, positionals, this, args.args); this.commandPath = args.commandPath; this.command = args.command; this.commandOptions = flags; debug(`Starting command with path ${args.commandPath}, command ${args.command}`); } // Entry point for runner. DO NOT override in command definition! execute() { return __awaiter(this, void 0, void 0, function* () { debug(`Initial execution of command`); if (this.help) { debug(`help switch detected, displaying help for command`); help_1.runHelp(Object.getPrototypeOf(this), this); return Result.success(); } if (this.version) { debug("Version switch detected, displaying version number"); return this.showVersion(); } if (this.debug) { interaction_1.setDebug(); const version = this.getVersion(); console.log(`Using appcenter-cli version: ${version}`); } if (this.quiet) { interaction_1.setQuiet(); } if (this.format) { if (this.format in this.additionalSupportedOutputFormats) { this.additionalSupportedOutputFormats[this.format](); } else if (this.format != null && this.format !== "") { return Promise.resolve(Result.failure(Result.ErrorCodes.InvalidParameter, `Unknown output format ${this.format}`)); } } this.clientFactory = apis_1.createAppCenterClient(this.command, yield profile_1.telemetryIsEnabled(this.disableTelemetry)); return this.runNoClient(); }); } // Entry point to load appcenter client. // Override this if your command needs to do something special with login - typically just // the login command runNoClient() { if (this.environmentName && !this.token) { return Promise.resolve(Result.failure(Result.ErrorCodes.IllegalCommand, "Cannot specify environment without giving token")); } let client; let endpoint; if (this.token) { debug(`Creating appcenter client for command from token for environment ${this.environmentName}`); [client, endpoint] = this.getClientAndEndpointForToken(this.environmentName, this.token); } else { // creating client for either logged in user or environment variable token const user = profile_1.getUser(); const tokenFromEnvVar = profile_1.getTokenFromEnvironmentVar(); const envFromEnvVar = profile_1.getEnvFromEnvironmentVar(); const isLogoutCommand = this.command[0] === "logout"; if (user && tokenFromEnvVar && !isLogoutCommand) { // logout command should be executed even if both user and env token are set - it just logs out user return Promise.resolve(Result.failure(Result.ErrorCodes.IllegalCommand, `logged in user and token in environment variable ${profile_1.appCenterAccessTokenEnvVar} cannot be used together`)); } else if (user) { debug(`Creating appcenter client for command for current logged in user`); client = this.clientFactory.fromProfile(user); endpoint = user.endpoint; } else if (tokenFromEnvVar) { debug(`Creating appcenter client from token specified in environment variable for environment ${this.environmentName}`); [client, endpoint] = this.getClientAndEndpointForToken(envFromEnvVar, tokenFromEnvVar); } } if (client && endpoint) { return this.run(client, profile_1.getPortalUrlForEndpoint(endpoint)); } return Promise.resolve(Result.notLoggedIn(`${misc_1.scriptName} ${this.command.join(" ")}`)); } // Entry point for command author - override this! run(client, portalBaseUrl) { throw new Error("Dev error, should be overridden!"); } showVersion() { interaction_1.out.text((s) => s, `${misc_1.scriptName} version ${this.getVersion()}`); return Result.success(); } getClientAndEndpointForToken(environmentString, token) { const environment = profile_1.environments(environmentString); if (!environment) { throw Result.failure(Result.ErrorCodes.InvalidParameter, `${environmentString} is not valid environment name`); } return [this.clientFactory.fromToken(token, environment.endpoint), environment.endpoint]; } getVersion() { const packageJsonPath = path.join(__dirname, "../../../package.json"); // eslint-disable-next-line security/detect-non-literal-require const packageJson = require(packageJsonPath); return packageJson.version; } fixArrayParameter(input) { if (!input) { return []; } else if (typeof input === "string") { return [input]; } return input; } } __decorate([ option_decorators_1.longName("debug"), option_decorators_1.help("Display extra output for debugging"), option_decorators_1.common ], Command.prototype, "debug", void 0); __decorate([ option_decorators_1.longName("output"), option_decorators_1.hasArg, option_decorators_1.help("Output format: json"), option_decorators_1.common ], Command.prototype, "format", void 0); __decorate([ option_decorators_1.longName("token"), option_decorators_1.hasArg, option_decorators_1.help("API token"), option_decorators_1.common ], Command.prototype, "token", void 0); __decorate([ option_decorators_1.longName("env"), option_decorators_1.hasArg, option_decorators_1.help("Environment when using API token"), option_decorators_1.common ], Command.prototype, "environmentName", void 0); __decorate([ option_decorators_1.shortName("h"), option_decorators_1.longName("help"), option_decorators_1.help("Display help for current command"), option_decorators_1.common ], Command.prototype, "help", void 0); __decorate([ option_decorators_1.longName("quiet"), option_decorators_1.help("Auto-confirm any prompts without waiting for input"), option_decorators_1.common ], Command.prototype, "quiet", void 0); __decorate([ option_decorators_1.shortName("v"), option_decorators_1.longName("version"), option_decorators_1.help(`Display ${misc_1.scriptName} version`), option_decorators_1.common ], Command.prototype, "version", void 0); __decorate([ option_decorators_1.longName("disable-telemetry"), option_decorators_1.help("Disable telemetry for this command"), option_decorators_1.common ], Command.prototype, "disableTelemetry", void 0); exports.Command = Command;