smartlog-destination-local/index.js

272 lines
9.7 KiB
JavaScript
Raw Normal View History

2016-01-25 00:01:07 +00:00
#!/usr/bin/env node
2015-11-01 21:00:07 +00:00
/// <reference path="./index.ts" />
2015-12-26 00:51:04 +00:00
var BeautylogPlugins;
(function (BeautylogPlugins) {
BeautylogPlugins.init = function () {
var plugins = {
smartenv: require("smartenv")
};
return plugins;
};
})(BeautylogPlugins || (BeautylogPlugins = {}));
/// <reference path="./index.ts" />
2015-12-20 22:14:22 +00:00
var tableHelpers = {
makeRow: function (cellCounterArg, colorArg) {
if (cellCounterArg === void 0) { cellCounterArg = 2; }
if (colorArg === void 0) { colorArg = "cyan"; }
var rowArray = [];
for (var i = 0; i < (cellCounterArg); i++) {
rowArray.push(String(i + 1).cyan);
}
return rowArray;
}
};
2015-11-15 05:15:20 +00:00
var ConsoleTable = (function () {
2015-12-20 22:14:22 +00:00
function ConsoleTable(tableTypeArg, tableHeadArrayArg) {
if (tableHeadArrayArg === void 0) { tableHeadArrayArg = tableHelpers.makeRow(); }
switch (tableTypeArg) {
2015-11-15 05:15:20 +00:00
case "checks":
this.tableHead = ['Check Item:'.cyan, 'Status:'.cyan];
break;
2015-12-20 22:14:22 +00:00
case "custom":
this.tableHead = tableHeadArrayArg;
break;
2015-11-15 05:15:20 +00:00
default:
break;
}
this.rows = [];
2015-12-20 22:14:22 +00:00
this.type = tableTypeArg;
2015-11-15 05:15:20 +00:00
}
ConsoleTable.prototype.push = function (row) {
this.rows.push(row);
};
ConsoleTable.prototype.print = function () {
2016-01-30 05:19:44 +00:00
var table = new BeautylogNodeTable.cliTable({
2015-12-20 22:14:22 +00:00
head: this.tableHead
2015-11-15 05:15:20 +00:00
});
for (var row in this.rows) {
if (this.rows[row][1] == "success") {
this.rows[row][1] = ' '.bgGreen + ' ' + this.rows[row][1];
}
else if (this.rows[row][1] == "error") {
this.rows[row][1] = ' '.bgRed + ' ' + this.rows[row][1];
}
table.push(this.rows[row]);
}
;
console.log(table.toString());
};
return ConsoleTable;
})();
/// <reference path="./index.ts" />
2015-12-26 00:51:04 +00:00
var BeautylogNode;
(function (BeautylogNode) {
2015-11-01 21:00:07 +00:00
function init() {
2016-01-30 05:19:44 +00:00
plugins.colors = require("colors");
plugins.clc = require("cli-color");
var beautylogNode = {
log: BeautylogNodeLog.init(),
code: BeautylogNodeCode.init()
}; //object to append to all public facing functions
2015-11-01 21:00:07 +00:00
/**
* logs an directory to console
* @param logText
* @returns {boolean}
*/
2015-12-26 00:51:04 +00:00
beautylogNode.dir = function (logText) {
return beautylogNode.log(logText, 'dir');
2015-11-01 21:00:07 +00:00
};
/**
* logs an error to console
* @param logText
* @returns {boolean}
*/
2015-12-26 00:51:04 +00:00
beautylogNode.error = function (logText) {
return beautylogNode.log(logText, 'error');
2015-11-01 21:00:07 +00:00
};
/**
* logs an info to console
* @param logText
* @returns {boolean}
*/
2015-12-26 00:51:04 +00:00
beautylogNode.info = function (logText) {
return beautylogNode.log(logText, 'info');
2015-11-01 21:00:07 +00:00
};
/**
* logs an 'OK!' message to console
* @param logText
* @returns {boolean}
*/
2015-12-26 00:51:04 +00:00
beautylogNode.ok = function (logText) {
return beautylogNode.log(logText, 'ok');
2015-11-01 21:00:07 +00:00
};
/**
* logs a success to console
* @param logText string to log as error
* @returns {boolean}
*/
2015-12-26 00:51:04 +00:00
beautylogNode.success = function (logText) {
return beautylogNode.log(logText, 'success');
2015-11-01 21:00:07 +00:00
};
/**
* logs a 'warn:' message to console
* @param logText string to log as error
* @returns {boolean}
*/
2015-12-26 00:51:04 +00:00
beautylogNode.warn = function (logText) {
return beautylogNode.log(logText, 'warn');
2015-11-01 21:00:07 +00:00
};
2016-01-30 05:19:44 +00:00
beautylogNode.table = BeautylogNodeTable.init();
2015-12-26 00:51:04 +00:00
return beautylogNode;
2015-09-20 19:45:37 +00:00
}
2015-12-26 00:51:04 +00:00
BeautylogNode.init = init;
})(BeautylogNode || (BeautylogNode = {}));
2015-11-01 21:00:07 +00:00
/// <reference path="./index.ts" />
2016-01-30 05:19:44 +00:00
var BeautylogNodeLog;
(function (BeautylogNodeLog) {
BeautylogNodeLog.init = function () {
var localBl = {
dirPrefix: plugins.clc.bgXterm(39).xterm(231).bold(' DIR ') + ' ',
errorPrefix: ' Error: '.bgRed.white.bold + ' ',
infoPrefix: plugins.clc.bgXterm(198).xterm(231).bold(' INFO ') + ' ',
normalPrefix: ' Log: '.bgCyan.white.bold + ' ',
okPrefix: ' '.bgGreen + ' OK! '.bgBlack.green.bold + ' ',
successPrefix: ' Success: '.bgGreen.white.bold + ' ',
warnPrefix: ' '.bgYellow + ' Warn: '.bgBlack.yellow.bold + ' '
};
/**
*
* @param logText
* @param logType
* @returns {boolean}
*/
var logFunction = function (logText, logType) {
if (logText === void 0) { logText = 'empty log'; }
if (logType === void 0) { logType = 'normal'; }
try {
switch (logType) {
case 'dir':
logText = localBl.dirPrefix + plugins.clc.xterm(26)(logText);
break;
case 'error':
logText = localBl.errorPrefix + logText.red.bold;
break;
case 'info':
logText = localBl.infoPrefix + plugins.clc.xterm(198)(logText);
break;
case 'normal':
logText = localBl.normalPrefix + logText.cyan.bold;
break;
case 'ok':
logText = localBl.okPrefix + logText.bold;
break;
case 'success':
logText = localBl.successPrefix + logText.green.bold;
break;
case 'warn':
logText = localBl.warnPrefix + logText.bold;
break;
case 'log':
default:
logText.blue.bold;
console.log(('unknown logType for "' + logText + '"').red.bold);
break;
}
console.log(logText);
return true;
}
catch (error) {
console.log(localBl.errorPrefix + 'You seem to have tried logging something strange'.red.bold + error);
return false;
}
};
return logFunction;
};
})(BeautylogNodeLog || (BeautylogNodeLog = {}));
/// <reference path="./index.ts" />
var BeautylogNodeCode;
(function (BeautylogNodeCode) {
BeautylogNodeCode.init = function () {
var codeFunction = function (codeString, options) {
2016-02-02 14:02:41 +00:00
var hlight = require("hlight");
2016-01-30 05:19:44 +00:00
var codeSnippet = {
source: codeString,
highlighted: "default"
};
if (typeof codeString != "string") {
console.log("beautylog.code() expects a string as first argument!");
return;
}
;
if (typeof options != "undefined") {
2016-02-02 14:02:41 +00:00
codeSnippet.highlighted = hlight(codeSnippet.source, options.language);
2016-01-30 05:19:44 +00:00
}
};
return codeFunction;
};
})(BeautylogNodeCode || (BeautylogNodeCode = {}));
/// <reference path="./index.ts" />
var BeautylogNodeTable;
(function (BeautylogNodeTable) {
2015-11-15 05:15:20 +00:00
function init() {
2016-01-30 05:19:44 +00:00
BeautylogNodeTable.cliTable = require("cli-table2");
2015-11-15 05:15:20 +00:00
var beautylogOsTable = {};
2015-12-20 22:14:22 +00:00
beautylogOsTable.new = function (typeArg, tableHeadArrayArg) {
var newConsoleTable = new ConsoleTable(typeArg, tableHeadArrayArg);
2015-11-15 05:15:20 +00:00
return newConsoleTable;
};
return beautylogOsTable;
}
2016-01-30 05:19:44 +00:00
BeautylogNodeTable.init = init;
})(BeautylogNodeTable || (BeautylogNodeTable = {}));
2015-11-15 05:15:20 +00:00
/// <reference path="./index.ts" />
2015-11-01 21:00:07 +00:00
var BeautylogBrowser;
(function (BeautylogBrowser) {
function init() {
var beautylogBrowser = {};
beautylogBrowser.log = function (message) {
console.log('%c Log: %c ' + message, "background:#42A5F5;color:#ffffff", "color:#42A5F5;");
};
beautylogBrowser.info = function (message) {
console.log('%c Info: %c ' + message, 'background:#EC407A;color:#ffffff;', 'color:#EC407A;');
};
beautylogBrowser.ok = function (message) {
console.log('%c OK: %c ' + message, "background:#000000;color:#8BC34A;", "color:#000000;");
};
beautylogBrowser.success = function (message) {
console.log('%c Success: %c ' + message, "background:#8BC34A;color:#ffffff;", "color:#8BC34A;");
};
beautylogBrowser.warn = function (message) {
console.log('%c Warn: %c ' + message, "background:#000000;color:#FB8C00;", "color:#000000;");
};
2015-11-01 21:07:29 +00:00
return beautylogBrowser;
2015-11-01 21:00:07 +00:00
}
BeautylogBrowser.init = init;
})(BeautylogBrowser || (BeautylogBrowser = {}));
2016-02-02 15:49:16 +00:00
/// <reference path="./typings/main.d.ts" />
2015-12-26 00:51:04 +00:00
/// <reference path="./beautylog.plugins.ts" />
2015-11-15 05:15:20 +00:00
/// <reference path="./beautylog.classes.ts" />
2015-12-26 00:51:04 +00:00
/// <reference path="./beautylog.node.ts" />
2016-01-30 05:19:44 +00:00
/// <reference path="./beautylog.node.log.ts" />
/// <reference path="./beautylog.node.code.ts" />
2015-12-26 00:51:04 +00:00
/// <reference path="./beautylog.node.table.ts" />
2015-11-15 05:15:20 +00:00
/// <reference path="./beautylog.browser.ts" />
2015-12-26 00:51:04 +00:00
var plugins = BeautylogPlugins.init();
var beautylog = (function () {
switch (plugins.smartenv.getEnv().runtimeEnv) {
case "node":
var beautylogOs = BeautylogNode.init();
2015-11-01 21:00:07 +00:00
return beautylogOs;
break;
case "browser":
var beautylogBrowser = BeautylogBrowser.init();
return beautylogBrowser;
break;
default:
2015-12-26 00:51:04 +00:00
console.log("something is strange about the platform in which you try to use beautylog");
2015-11-01 21:00:07 +00:00
break;
2015-09-21 19:50:52 +00:00
}
2015-12-26 00:51:04 +00:00
})();
2015-11-01 21:00:07 +00:00
module.exports = beautylog;