Files
bower/lib/renderers/JsonRenderer.js
André Cruz 454436905c Huge commit.
- Changed way renderers work
- Move worker to a separate module
- Improve loglevel
- Minor tweaks
- Fix tests
2013-05-27 22:59:13 +01:00

50 lines
1.0 KiB
JavaScript

function JsonRenderer() {
this._nrNotifications = 0;
}
JsonRenderer.prototype.end = function (data) {
if (this._nrNotifications) {
process.stderr.write(']\n');
}
if (data) {
process.stdout.write(this._stringify(data) + '\n');
}
};
JsonRenderer.prototype.error = function (err) {
err.id = err.code || 'error';
err.level = 'error';
this.notification(err);
this.end();
};
JsonRenderer.prototype.notification = function (notification) {
if (!this._nrNotifications) {
process.stderr.write('[');
} else {
process.stderr.write(', ');
}
process.stderr.write(this._stringify(notification));
this._nrNotifications++;
};
// -------------------------
JsonRenderer.prototype.updateAvailable = function () {};
// -------------------------
JsonRenderer.prototype._stringify = function (notification) {
// To json
var str = JSON.stringify(notification, null, ' ');
// Remove colors
str = str.replace(/\x1B\[\d+m/g, '');
return str;
};
module.exports = JsonRenderer;