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.

76 lines
1.6 KiB
JavaScript

'use strict';
const path = require("path");
const chalk = require("chalk");
const spy = require("through2-spy");
const fancyLog = require("fancy-log");
const padWidth = 5;
const typeColors = {
error: "red",
warn: "yellow",
info: "cyan",
debug: "gray"
}
function hexify(buff) {
let bytes = [];
for (let i = 0; i < buff.length; i++) {
bytes.push(buff.toString("hex", i, i + 1));
}
return bytes;
}
module.exports = function namedLog(name, options = {}) {
let prefix = `[${chalk.green(name)}]`;
function createLog(type) {
let prefixes = [prefix];
if (type) {
let typeColor = (typeColors[type] != null) ? typeColors[type] : "white";
let typePrefix = `[${chalk[typeColor](type)}]`;
prefixes.push(typePrefix);
}
return function log(...args) {
fancyLog.apply(null, prefixes.concat(args));
}
}
return {
debug: createLog("debug"),
info: createLog("info"),
warn: createLog("warn"),
error: createLog("error"),
log: createLog(),
stream: function() {
return spy.obj((file) => {
let shortPath, firstBytes, suffix;
let 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} >>`);
});
}
}
}