mirror of
https://github.com/bower/bower.git
synced 2026-01-22 20:58:08 -05:00
56 lines
1.2 KiB
JavaScript
56 lines
1.2 KiB
JavaScript
function JsonRenderer() {
|
|
this._nrLogs = 0;
|
|
}
|
|
|
|
JsonRenderer.prototype.end = function (data) {
|
|
if (this._nrLogs) {
|
|
process.stderr.write(']\n');
|
|
}
|
|
|
|
if (data) {
|
|
process.stdout.write(this._stringify(data) + '\n');
|
|
}
|
|
};
|
|
|
|
JsonRenderer.prototype.error = function (err) {
|
|
var message = err.message;
|
|
|
|
err.id = err.code || 'error';
|
|
err.level = 'error';
|
|
err.data = err.data || {};
|
|
|
|
// Need to set message again because it is
|
|
// not enumerable in some cases
|
|
delete err.message;
|
|
err.message = message;
|
|
|
|
this.log(err);
|
|
this.end();
|
|
};
|
|
|
|
JsonRenderer.prototype.log = function (log) {
|
|
if (!this._nrLogs) {
|
|
process.stderr.write('[');
|
|
} else {
|
|
process.stderr.write(', ');
|
|
}
|
|
|
|
process.stderr.write(this._stringify(log));
|
|
this._nrLogs++;
|
|
};
|
|
|
|
JsonRenderer.prototype.updateAvailable = function () {};
|
|
|
|
// -------------------------
|
|
|
|
JsonRenderer.prototype._stringify = function (log) {
|
|
// To json
|
|
var str = JSON.stringify(log, null, ' ');
|
|
// Remove colors in case some log has colors..
|
|
str = str.replace(/\x1B\[\d+m/g, '');
|
|
|
|
return str;
|
|
};
|
|
|
|
module.exports = JsonRenderer;
|