trm-commons
Version:
TRM (Transport Request Manager) Shared library
228 lines (227 loc) • 6.99 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CliLogger = void 0;
const loading_cli_1 = __importDefault(require("loading-cli"));
const cli_table3_1 = __importDefault(require("cli-table3"));
const trm_registry_types_1 = require("trm-registry-types");
const printTree = __importStar(require("print-tree"));
const chalk_1 = __importDefault(require("chalk"));
class CliLogger {
constructor(debug) {
this._prefix = '';
this._cliObj = (0, loading_cli_1.default)({
frames: ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
interval: 200
});
}
loading(text, debug) {
if (debug && !this.debug) {
return;
}
this._loader = this._cliObj.render().start(this._prefix + text);
}
success(text, debug) {
if (debug && !this.debug) {
return;
}
const aText = text.split('\n');
aText.forEach(s => {
s = chalk_1.default.green(this._prefix + s);
if (this._loader) {
this._loader.succeed(s);
this._clearLoader();
}
else {
this._cliObj.succeed(s);
}
});
}
error(text, debug) {
if (debug && !this.debug) {
return;
}
const aText = text.split('\n');
aText.forEach(s => {
s = chalk_1.default.red(this._prefix + s);
if (this._loader) {
this._loader.fail(s);
this._clearLoader();
}
else {
this._cliObj.fail(s);
}
});
}
warning(text, debug) {
if (debug && !this.debug) {
return;
}
const aText = text.split('\n');
aText.forEach(s => {
s = chalk_1.default.yellow(this._prefix + s);
if (this._loader) {
this._loader.warn(s);
this._clearLoader();
}
else {
this._cliObj.warn(s);
}
});
}
info(text, debug) {
if (debug && !this.debug) {
return;
}
const aText = text.split('\n');
aText.forEach(s => {
s = this._prefix + s;
if (this._loader) {
this._loader.info(s);
this._clearLoader();
}
else {
this._cliObj.info(s);
}
});
}
log(text, debug) {
if (debug && !this.debug) {
return;
}
const aText = text.split('\n');
aText.forEach(s => {
s = chalk_1.default.dim(this._prefix + s);
if (this._loader) {
this.forceStop();
}
console.log(s);
});
}
table(header, data, debug) {
if (debug && !this.debug) {
return;
}
var table = new cli_table3_1.default({
head: header,
chars: { 'top': '═', 'top-mid': '╤', 'top-left': '╔', 'top-right': '╗',
'bottom': '═', 'bottom-mid': '╧', 'bottom-left': '╚', 'bottom-right': '╝',
'left': '║', 'left-mid': '╟', 'mid': '─', 'mid-mid': '┼',
'right': '║', 'right-mid': '╢', 'middle': '│' }
});
data.forEach(arr => {
table.push(arr);
});
console.log(this._prefix + table.toString());
}
registryResponse(response, debug) {
if (debug && !this.debug) {
return;
}
if (response.type === trm_registry_types_1.MessageType.ERROR) {
this.error(response.text, debug);
}
if (response.type === trm_registry_types_1.MessageType.INFO) {
this.info(response.text, debug);
}
if (response.type === trm_registry_types_1.MessageType.WARNING) {
this.warning(response.text, debug);
}
}
tree(data, debug) {
if (debug && !this.debug) {
return;
}
const _parseBranch = (o) => {
var children = [];
o.children.forEach(k => {
children.push(_parseBranch(k));
});
return {
name: o.text,
children
};
};
const treeData = _parseBranch(data);
this.forceStop();
printTree.default(treeData, (node) => {
return node.name;
}, (node) => {
return node.children;
});
}
forceStop() {
try {
this._loader.stop();
this._clearLoader();
}
catch (e) { }
}
_clearLoader() {
delete this._loader;
}
setPrefix(text) {
this._prefix = text;
}
removePrefix() {
this._prefix = '';
}
getPrefix() {
return this._prefix;
}
msgty(msgty, text, debug) {
switch (msgty) {
case 'A':
this.error(text, debug);
break;
case 'E':
this.error(text, debug);
break;
case 'I':
this.info(text, debug);
break;
case 'S':
this.success(text, debug);
break;
case 'W':
this.warning(text, debug);
break;
}
}
}
exports.CliLogger = CliLogger;