Better Vinyl stream logging

master
Sven Slootweg 8 years ago
parent 165d408a6d
commit 8ec221fc80

@ -1,5 +1,6 @@
'use strict';
var path = require("path");
var chalk = require("chalk");
var spy = require("through2-spy");
var fancyLog = require("fancy-log");
@ -13,7 +14,19 @@ var typeColors = {
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;
}
module.exports = function namedLog(name) {
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var prefix = "[" + chalk.green(name) + "]";
function createLog(type) {
@ -45,7 +58,27 @@ module.exports = function namedLog(name) {
var _this = this;
return spy.obj(function (file) {
_this.info(file.path, file.contents);
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 + " >>");
});
}
};

@ -1,5 +1,6 @@
'use strict';
const path = require("path");
const chalk = require("chalk");
const spy = require("through2-spy");
const fancyLog = require("fancy-log");
@ -13,7 +14,17 @@ const typeColors = {
debug: "gray"
}
module.exports = function namedLog(name) {
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) {
@ -39,7 +50,25 @@ module.exports = function namedLog(name) {
log: createLog(),
stream: function() {
return spy.obj((file) => {
this.info(file.path, file.contents);
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} >>`);
});
}
}

Loading…
Cancel
Save