UNPKG

tia

Version:

Time is All (logs driven test engine with ExtJs support)

167 lines 4.12 kB
"use strict"; /** * Inner utilities for logging into console. * gIn.cLogger. */ Object.defineProperty(exports, "__esModule", { value: true }); let chalk; // eslint-disable-line @typescript-eslint/no-explicit-any let isChalkEnabled = false; // Just to speed up checking boolean instead of Boolean(object). if (!process.env.TIA_NO_COLORS) { process.env.FORCE_COLOR = '1'; chalk = require('chalk'); isChalkEnabled = true; } /** * Tracks EOL of last message printed to console. * Also msg can be boolean - true means there is EOL. * @param msg */ function trackEOL(msg) { if (typeof msg === 'string') { // eslint-disable-next-line @typescript-eslint/prefer-regexp-exec if (msg.match(/(\n|\r)$/)) { gIn.tracePrefix = ''; } else { gIn.tracePrefix = '\n'; } } else { if (msg) { gIn.tracePrefix = ''; } else { gIn.tracePrefix = '\n'; } } } /** * Writes message to stdout as is. * @param message */ function msg(message) { process.stdout.write(message); trackEOL(message); } exports.msg = msg; function msgln(message) { msg(`${message}\n`); } exports.msgln = msgln; function logResourcesUsage(prefix = '') { // if (gT.config.resUsagePrintAtErrors) { msgln(prefix + gT.nodeUtils.getResourcesUsage(true)); // } } exports.logResourcesUsage = logResourcesUsage; /** * * @param chalkProps - string or array. * @param message * @returns {*} */ function chalkWrap(chalkProps, message) { let resMsg = message; if (isChalkEnabled) { if (typeof chalkProps === 'string') { resMsg = chalk[chalkProps](resMsg); } else { chalkProps.forEach(prop => { resMsg = chalk[prop](resMsg); }); } } return resMsg; } exports.chalkWrap = chalkWrap; /** * Writes string from dif to console. * @param message */ function msgDifStr(message) { process.stdout.write(chalkWrap(['yellow', 'bold'], message)); trackEOL(message); } exports.msgDifStr = msgDifStr; /** * Writes string for debug tracing. * @param message */ function msgDbg(message) { process.stdout.write(`${chalkWrap(['cyan', 'bold'], message)}\n`); trackEOL(true); } exports.msgDbg = msgDbg; /** * Writes msg to stdout using red ANSI color code. * @param message */ function err(message) { let resMsg = message; if (isChalkEnabled) { resMsg = chalk.red(resMsg); } process.stdout.write(resMsg); trackEOL(resMsg); } exports.err = err; function errln(message) { err(`${message}\n`); } exports.errln = errln; // ===================================== /** * Writes msg to stdout if corresponding parameter is specified in cmd line. * Otherwise - does nothing. * @param message */ function logIfEnabled(message) { if (gT.cLParams.logToConsole) { msg(gIn.loggerCfg.consoleLogPrefix + message); } } exports.logIfEnabled = logIfEnabled; /** * * @param message * Prefix should be set in caller. */ function errIfEnabled(message) { if (gT.cLParams.errToConsole) { return err(message); } } exports.errIfEnabled = errIfEnabled; function passIfEnabled(message) { let resMsg = message; if (gT.cLParams.logToConsole) { if (isChalkEnabled) { resMsg = chalk.green(resMsg); } msg(gIn.loggerCfg.consoleLogPrefix + resMsg); } } exports.passIfEnabled = passIfEnabled; function failIfEnabled(message) { let resMsg = message; if (gT.cLParams.logToConsole) { if (isChalkEnabled) { resMsg = chalk.red(resMsg); } msg(gIn.loggerCfg.consoleLogPrefix + resMsg); } } exports.failIfEnabled = failIfEnabled; // ===================================== function logBold(message) { let resMsg = message; if (gT.cLParams.logToConsole) { if (isChalkEnabled) { resMsg = chalk.bold(resMsg); } msg(gIn.loggerCfg.consoleLogPrefix + resMsg); } } exports.logBold = logBold; //# sourceMappingURL=console-logger.js.map