UNPKG

@appium/support

Version:

Support libs used across Appium packages

104 lines 3.69 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.symbols = exports.console = exports.CliConsole = void 0; const lodash_1 = __importDefault(require("lodash")); const supports_color_1 = require("supports-color"); const node_console_1 = require("node:console"); require("@colors/colors"); const log_symbols_1 = __importDefault(require("log-symbols")); exports.symbols = log_symbols_1.default; const node_stream_1 = require("node:stream"); /** * Stream to nowhere. Used when we want to disable any output other than JSON output. */ class NullWritable extends node_stream_1.Writable { /* eslint-disable promise/prefer-await-to-callbacks -- Node stream callback API */ _write(chunk, encoding, callback) { setImmediate(callback); } } /** * A particular console/logging class for Appium's CLI. * * - By default, uses some fancy symbols * - Writes to `STDERR`, generally. * - In "JSON mode", `STDERR` is squelched. Use {@linkcode CliConsole.json} to write the JSON. * * DO NOT extend this to do anything other than what it already does. Download a library or something. */ class CliConsole { #console; #useSymbols; #useColor; static symbolToColor = { success: 'green', info: 'cyan', warning: 'yellow', error: 'red', }; constructor(opts = {}) { const { jsonMode = false, useSymbols = true, useColor } = opts; this.#console = new node_console_1.Console(process.stdout, jsonMode ? new NullWritable() : process.stderr); this.#useSymbols = Boolean(useSymbols); this.#useColor = Boolean(useColor ?? (0, supports_color_1.createSupportsColor)(process.stderr)); } /** * Wraps a message string in breathtaking fanciness * * Returns `undefined` if `msg` is `undefined`. */ decorate(msg, symbol) { if (!lodash_1.default.isString(msg) || !lodash_1.default.isString(symbol) || !this.#useSymbols) { return msg; } let newMsg = `${log_symbols_1.default[symbol]} ${msg}`; if (this.#useColor) { const color = CliConsole.symbolToColor[symbol]; // @colors/colors extends String with color getters (e.g. .green, .red) newMsg = newMsg[color] ?? newMsg; } return newMsg; } /** * Writes to `STDOUT`. Must be stringifyable. * * You probably don't want to call this more than once before exiting (since that will output invalid JSON). */ json(value) { this.#console.log(JSON.stringify(value)); } /** General logging function. */ log(message, ...args) { this.#console.error(message, ...args); } /** A "success" message */ ok(message, ...args) { this.#console.error(this.decorate(message, 'success'), ...args); } /** Alias for {@linkcode CliConsole.log} */ debug(message, ...args) { this.log(message, ...args); } /** Wraps {@link console.dir} */ dump(item, opts) { this.#console.dir(item, opts); } /** An "info" message */ info(message, ...args) { this.log(this.decorate(message, 'info'), ...args); } /** A "warning" message */ warn(message, ...args) { this.log(this.decorate(message, 'warning'), ...args); } /** An "error" message */ error(message, ...args) { this.log(this.decorate(message, 'error'), ...args); } } exports.CliConsole = CliConsole; exports.console = new CliConsole(); //# sourceMappingURL=console.js.map