Files
bower/lib/renderers/json.js
2013-05-24 12:33:08 +01:00

39 lines
803 B
JavaScript

function uncolor(str) {
return str.replace(/\x1B\[\d+m/g, '');
}
function stringify(data) {
return uncolor(JSON.stringify(data, null, ' '));
}
// -------------------------
var nrData = 0;
// In the json output, everything goes to stderr except
// the final command result that goes to stdout.
var json = {
begin: function () {
process.stderr.write('[');
},
end: function (data) {
process.stderr.write(']\n');
if (data) {
process.stdout.write(stringify(data) + '\n');
}
},
error: function (err) {
this.data(err);
},
data: function (data) {
if (nrData) {
process.stderr.write(', ');
}
process.stderr.write(stringify(data));
nrData++;
}
};
module.exports = json;