solace
Version:
Alternative to console log which automatically beautifies the output
88 lines (71 loc) • 2.59 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', {
value: true
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _chalk = require('chalk');
var _chalk2 = _interopRequireDefault(_chalk);
var stringify = function stringify(value) {
return JSON.stringify(value, null, ' ') + '\n';
};
var asTitle = function asTitle(title) {
return _chalk2['default'].cyan(title);
};
var asError = function asError(text) {
return _chalk2['default'].red(text);
};
exports['default'] = {
name: 'beautify',
title: 'Format the output in a beautiful manner',
writeError: function writeError(messager, message, options) {
var hasTitle = _lodash2['default'].isString(options.title);
if (hasTitle) {
messager.write(asTitle(options.title) + ': ' + asError(message) + '\n');
} else {
messager.write(asError(message) + '\n');
}
}, //end write
writeSimpleValue: function writeSimpleValue(messager, message, options) {
var hasTitle = _lodash2['default'].isString(options.title);
if (hasTitle) {
messager.write(asTitle(options.title) + (': ' + message + '\n'));
} else {
messager.write(message + '\n');
}
}, //end write
writeObject: function writeObject(messager, message, options) {
var hasTitle = _lodash2['default'].isString(options.title);
if (hasTitle) {
messager.write(asTitle(options.title) + ':\n\n' + stringify(message));
} else {
messager.write(stringify(message));
}
},
writeEmptyArray: function writeEmptyArray(messager, message, options) {
var hasTitle = _lodash2['default'].isString(options.title);
if (hasTitle) {
messager.write(asTitle(options.title) + ': ' + stringify(message));
} else {
messager.write(stringify(message));
}
},
writeArrayOfSimpleValues: function writeArrayOfSimpleValues(messager, message, options) {
var hasTitle = _lodash2['default'].isString(options.title);
if (hasTitle) {
messager.write(asTitle(options.title) + ':\n\n' + stringify(message));
} else {
messager.write(stringify(message));
}
},
writeArrayOfSimpleObjects: function writeArrayOfSimpleObjects(messager, message, options) {
var hasTitle = _lodash2['default'].isString(options.title);
if (hasTitle) {
messager.write(asTitle(options.title) + ':\n\n' + stringify(message));
} else {
messager.write(stringify(message));
}
}
};
module.exports = exports['default'];