UNPKG

@adpt/cli

Version:
193 lines 6.4 kB
"use strict"; /* * Copyright 2019-2020 Unbounded Systems, LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("@adpt/utils"); const command_1 = require("@oclif/command"); const stream_1 = require("stream"); const error_1 = require("../error"); const outputSettings = { pretty: { listrOptions: { renderer: "update", collapse: false, }, logging: false, statusOutput: true, }, notty: { listrOptions: { renderer: "verbose", }, logging: true, statusOutput: true, }, interactive: { listrOptions: { renderer: "verbose", }, logging: false, statusOutput: true, }, quiet: { listrOptions: { renderer: "silent", }, logging: false, statusOutput: false, }, }; function getOutputSettings(theFlags, interactive = false) { let output = process.stdout.isTTY ? "pretty" : "notty"; if (interactive) { output = "interactive"; } else { if (theFlags.quiet) output = "quiet"; // If debugs are enabled, override pretty if (output === "pretty" && (theFlags.debug || process.env.DEBUG)) { output = "notty"; } } const settings = outputSettings[output]; if (!settings) throw new error_1.UserError(`Invalid --output type "${output}"`); return settings; } exports.getOutputSettings = getOutputSettings; const defaultHandleResponseOptions = { errorStart: "", errorEnding: "", }; class AdaptBase extends command_1.Command { constructor() { super(...arguments); this.finalOutput = []; this._interactive = false; } get args() { if (this._args == null) throw new utils_1.InternalError(`_args is null`); return this._args; } get cmdArgv() { if (this._cmdArgv == null) throw new utils_1.InternalError(`_cmdArgv is null`); return this._cmdArgv; } get outputSettings() { if (!this.outputSettings_) { throw new utils_1.InternalError(`must call this.parse before accessing outputSettings`); } return this.outputSettings_; } // This allows retrieval of flags with correct typings (or at least as // correct as oclif currently provides us). // The argument is the derived class constructor (which contains the // flags configuration from which the types are derived). flags(_) { if (this._flags == null) throw new utils_1.InternalError(`_flags is null`); return this._flags; } get interactive() { return this._interactive; } set interactive(val) { this.outputSettings_ = getOutputSettings(this.flags(AdaptBase), val); } async finally(err) { await super.finally(err); let output = this.finalOutput .filter((o) => (err === undefined) || o.outputOnError) .map((o) => o.text) .join("\n"); if (output) { if (this.outputSettings_ && this.outputSettings_.statusOutput === true) { output = "\n" + output; } if (err !== undefined) output += "\n"; this.log(output); } if (utils_1.isUserError(err)) return this.error(err.userError); } appendOutput(text, outputOnError = false) { this.finalOutput.push({ text, outputOnError }); } /* Synonym for handleApiResponse */ isApiSuccess(response, options = {}) { return this.handleApiResponse(response, options); } handleApiResponse(response, options = {}) { const { errorStart, errorEnding } = Object.assign({}, defaultHandleResponseOptions, options); const action = options.action ? ` during ${options.action}` : ""; const nwarn = response.summary.warning; if (nwarn > 0) { const warns = nwarn === 1 ? "warning" : "warnings"; this.appendOutput(`${nwarn} ${warns} encountered${action}:\n` + utils_1.getWarnings(response.messages), true); } const nerr = response.summary.error; if (nerr > 0) { const errors = nerr === 1 ? "error" : "errors"; let msg = errorStart + `${nerr} ${errors} encountered${action}:\n` + utils_1.getErrors(response.messages); if (errorEnding) msg += "\n" + errorEnding; return this.error(msg); } if (response.type === "error") { return this.error(errorStart + `Internal error: error response received with no error message`); } return true; } parse(options, argv) { const ret = super.parse(options, argv); this._flags = ret.flags; this._args = ret.args; this._cmdArgv = ret.argv; this.outputSettings_ = getOutputSettings(ret.flags); return ret; } } AdaptBase.flags = { quiet: command_1.flags.boolean({ allowNo: false, char: "q", description: "Suppress status output messages. Still outputs any result " + "output.", }), }; exports.AdaptBase = AdaptBase; function createLoggerPair(loggerId, logging) { const thru = new stream_1.PassThrough(); const client = new utils_1.MessageStreamClient({ inputStream: thru, outStream: logging ? process.stdout : undefined, errStream: logging ? process.stderr : undefined, }); const logger = new utils_1.MessageStreamServer(loggerId, { outStream: thru, }); return { client, logger, }; } exports.createLoggerPair = createLoggerPair; //# sourceMappingURL=adapt_base.js.map