You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.5 KiB
JavaScript

'use strict';
var util = require("util");
var path = require("path");
var chalk = require("chalk");
var spy = require("through2-spy");
var fancyLog = require("fancy-log");
var repeatString = require("repeat-string");
var padWidth = 5;
var typeColors = {
error: "red",
warn: "yellow",
info: "cyan",
debug: "gray"
};
function hexify(buff) {
var bytes = [];
for (var i = 0; i < buff.length; i++) {
bytes.push(buff.toString("hex", i, i + 1));
}
return bytes;
}
function renderError(err) {
var message = void 0,
stack = void 0;
if (err.stack != null) {
var stackLines = err.stack.split("\n");
message = stackLines[0];
stack = stackLines.slice(1);
} else {
message = err.name + ": " + err.message;
}
var properties = Object.keys(err).map(function (key) {
if (["name", "message", "stack"].indexOf(key) === -1) {
return {
key: key,
value: err[key]
};
}
}).filter(function (item) {
return item != null;
});
if (stack != null) {
properties.push({
key: "stacktrace",
literal: stack.map(function (line) {
return line.trim();
}).join("\n")
});
}
var renderedProperties = properties.map(function (property) {
var renderedValue = void 0;
if (property.literal != null) {
renderedValue = property.literal;
} else if (typeof property.value === "string") {
renderedValue = property.value;
} else {
renderedValue = util.inspect(property.value, { colors: true });
}
return chalk.bold.blue(property.key) + ":\n" + indentMultiline(renderedValue);
}).join("\n");
var details = indentMultiline(renderedProperties);
return chalk.bold.red(message) + "\n" + details;
}
function indentMultiline(text) {
var spaces = arguments.length <= 1 || arguments[1] === undefined ? 4 : arguments[1];
var indent = repeatString(" ", spaces);
return text.split("\n").map(function (line) {
return indent + line;
}).join("\n");
}
module.exports = function namedLog(name) {
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var prefix = "[" + chalk.green(name) + "]";
function createLog(type) {
var prefixes = [prefix];
if (type) {
var typeColor = typeColors[type] != null ? typeColors[type] : "white";
var typePrefix = "[" + chalk[typeColor](type) + "]";
prefixes.push(typePrefix);
}
return function log() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
if (args[0] instanceof Error) {
var renderedError = renderError(args[0]);
fancyLog.apply(null, prefixes.concat([renderedError]));
} else {
fancyLog.apply(null, prefixes.concat(args));
}
};
}
return {
debug: createLog("debug"),
info: createLog("info"),
warn: createLog("warn"),
error: createLog("error"),
log: createLog(),
stream: function stream() {
var _this = this;
return spy.obj(function (file) {
var shortPath = void 0,
firstBytes = void 0,
suffix = void 0;
var byteLimit = options.byteLimit != null ? options.byteLimit : 20;
if (options.basePath != null) {
shortPath = path.relative(options.basePath, file.path);
} else {
shortPath = path.basename(file.path);
}
if (file.contents.length <= byteLimit) {
firstBytes = file.contents;
suffix = "";
} else {
firstBytes = file.contents.slice(0, byteLimit);
suffix = " ...";
}
_this.info("<File \"" + shortPath + "\" <Buffer " + hexify(firstBytes).join(" ") + " " + suffix + " >>");
});
}
};
};