mirror of
https://github.com/bower/bower.git
synced 2026-02-12 06:55:04 -05:00
Move stdout/stderr logic to the renderers to allow for greater flexibility.
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
var mout = require('mout');
|
||||
|
||||
var colorful;
|
||||
var colorless;
|
||||
var paddings = {
|
||||
tag: 10,
|
||||
tagPlusLabel: 31
|
||||
@@ -9,67 +7,23 @@ var paddings = {
|
||||
var tagColors = {
|
||||
'warn': 'yellow',
|
||||
'error': 'red',
|
||||
'_default': 'cyan',
|
||||
'default': 'cyan',
|
||||
};
|
||||
|
||||
function renderData(data) {
|
||||
// Ensure data
|
||||
data.data = data.data || '';
|
||||
|
||||
return 'bower ' + renderTagPlusLabel(data) + ' ' + data.data + '\n';
|
||||
}
|
||||
|
||||
function renderError(err) {
|
||||
var str;
|
||||
|
||||
err.level = 'error';
|
||||
err.tag = 'error';
|
||||
|
||||
str = 'bower ' + renderTagPlusLabel(err) + ' ' + (err.code ? err.code + ' ,' : '') + err.message + '\n';
|
||||
|
||||
// Check if additional details were provided
|
||||
if (err.details) {
|
||||
str += err.details + '\n';
|
||||
}
|
||||
|
||||
// Print stack
|
||||
str += '\n' + err.stack + '\n';
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
function renderEnd() {
|
||||
return '';
|
||||
}
|
||||
|
||||
function renderCheckout(data) {
|
||||
if (isCompact()) {
|
||||
data.data = data.origin + '#' + data.data;
|
||||
}
|
||||
|
||||
return renderData(data);
|
||||
}
|
||||
|
||||
// -------------------------
|
||||
|
||||
function empty() {
|
||||
return '';
|
||||
function isCompact() {
|
||||
return process.stdout.columns < 120;
|
||||
}
|
||||
|
||||
function uncolor(str) {
|
||||
return str.replace(/\x1B\[\d+m/g, '');
|
||||
}
|
||||
|
||||
function isCompact() {
|
||||
return process.stdout.columns < 120;
|
||||
}
|
||||
|
||||
function renderTagPlusLabel(data) {
|
||||
var label;
|
||||
var length;
|
||||
var nrSpaces;
|
||||
var tag = data.tag;
|
||||
var tagColor = tagColors[data.level] || tagColors._default;
|
||||
var tagColor = tagColors[data.level] || tagColors['default'];
|
||||
|
||||
// If there's not enough space, print only the tag
|
||||
if (isCompact()) {
|
||||
@@ -80,31 +34,57 @@ function renderTagPlusLabel(data) {
|
||||
length = tag.length + label.length + 1;
|
||||
nrSpaces = paddings.tagPlusLabel - length;
|
||||
|
||||
// Ensure at least one space
|
||||
// Ensure at least one space between the label and the tag
|
||||
if (nrSpaces < 1) {
|
||||
nrSpaces = 1;
|
||||
}
|
||||
|
||||
|
||||
return label.green + mout.string.repeat(' ', nrSpaces) + tag[tagColor];
|
||||
}
|
||||
|
||||
// -------------------------
|
||||
|
||||
colorful = {
|
||||
head: empty,
|
||||
tail: empty,
|
||||
data: renderData,
|
||||
error: renderError,
|
||||
end: renderEnd,
|
||||
checkout: renderCheckout
|
||||
var colorful = {
|
||||
begin: function () {},
|
||||
end: function () {},
|
||||
error: function (err) {
|
||||
var str;
|
||||
|
||||
str = 'bower ' + renderTagPlusLabel(err) + ' ' + (err.code ? err.code + ' ,' : '') + err.message + '\n';
|
||||
|
||||
// Check if additional details were provided
|
||||
if (err.details) {
|
||||
str += err.details + '\n';
|
||||
}
|
||||
|
||||
// Print stack
|
||||
str += '\n' + err.stack + '\n';
|
||||
|
||||
this._write(process.stderr, str);
|
||||
},
|
||||
data: function (data) {
|
||||
data.data = data.data || '';
|
||||
|
||||
this._write(process.stdout, 'bower ' + renderTagPlusLabel(data) + ' ' + data.data + '\n');
|
||||
},
|
||||
checkout: function (data) {
|
||||
if (isCompact()) {
|
||||
data.data = data.origin + '#' + data.data;
|
||||
}
|
||||
|
||||
this.data(data);
|
||||
},
|
||||
_write: function (channel, str) {
|
||||
channel.write(str);
|
||||
}
|
||||
};
|
||||
|
||||
// The colorless variant simply removes the colors from the colorful methods
|
||||
colorless = mout.object.map(module.exports.colorful, function (fn) {
|
||||
return function () {
|
||||
var str = fn.apply(fn, arguments);
|
||||
return uncolor(str);
|
||||
};
|
||||
// The colorless variant simply removes the colors from the write method
|
||||
var colorless = mout.object.mixIn({}, colorful, {
|
||||
_write: function (channel, str) {
|
||||
channel.write(uncolor(str));
|
||||
}
|
||||
});
|
||||
|
||||
module.exports.colorful = colorful;
|
||||
|
||||
Reference in New Issue
Block a user