fakeit
Version:
Command-line utility that generates fake data which can be output as JSON, YAML, CSON, or CSV formats based on models defined in YAML.
290 lines (229 loc) • 8.81 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _toJs = require('to-js');
var _toJs2 = _interopRequireDefault(_toJs);
var _chalk = require('chalk');
var _chalk2 = _interopRequireDefault(_chalk);
var _logSymbols = require('log-symbols');
var _logSymbols2 = _interopRequireDefault(_logSymbols);
var _perfy = require('perfy');
var _perfy2 = _interopRequireDefault(_perfy);
var _ora = require('ora');
var _ora2 = _interopRequireDefault(_ora);
var _formatSeconds = require('format-seconds');
var _formatSeconds2 = _interopRequireDefault(_formatSeconds);
var _eventsAsync = require('events-async');
var _eventsAsync2 = _interopRequireDefault(_eventsAsync);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
_logSymbols2.default.warn = _logSymbols2.default.warning;
_logSymbols2.default.ok = _logSymbols2.default.okay = _logSymbols2.default.success;
/// @name Logger
/// @description
/// This is the main logger for the application
var Logger = function (_Emitter) {
(0, _inherits3.default)(Logger, _Emitter);
///# @name constructor
///# @arg {object} options [{ log: true, verbose: false, timestamp: true }]
function Logger() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
(0, _classCallCheck3.default)(this, Logger);
var _this = (0, _possibleConstructorReturn3.default)(this, (Logger.__proto__ || (0, _getPrototypeOf2.default)(Logger)).call(this));
_this.setMaxListeners(50);
_this.options = _toJs2.default.extend({
log: true,
verbose: false,
timestamp: true,
spinners: true
}, options);
// ensure that if `verbose` is true then `log` must also be true
if (_this.options.verbose) {
_this.options.log = true;
}
if (!_this.options.log) {
_this.options.spinners = false;
}
///# @name log_types
///# @static
///# @type {object}
///# @raw-code
_this.log_types = {
error: 'red',
warning: 'yellow',
success: 'green', // possibly remove
info: 'blue',
verbose: 'magenta',
log: 'gray'
};
_this.spinners = {};
return _this;
}
///# @name log
///# @description This is used to control the logging of an app
///# @arg {*} type
///# If this is a `string` and matches `error`, `warning`, `success`, `info`, `verbose`, `log` then it
///# will add special characters before the rest of the log depending on the type that was passed.
///# If it's not one of these then the type will default to `log` and the value you passed will be
///# prepended to the rest of the arguments
///# @arg {*} arg - What will be logged;
///# @chainable
(0, _createClass3.default)(Logger, [{
key: 'log',
value: function log(type, arg) {
if (type instanceof Error) {
arg = type;
type = 'error';
}
if (this.options.log || type === 'error') {
if (['time', 'timeEnd'].indexOf(type) !== -1) {
return this[type](arg);
}
if (!(_toJs2.default.keys(this.log_types).indexOf(type) !== -1)) {
arg = type;
type = 'log';
}
if (type === 'verbose') {
if (!this.options.verbose) return;
type = 'console';
}
var stamp = this.stamp(type);
// print the current time.
if (stamp) {
process.stdout.write(stamp);
}
console.log(arg);
if (type === 'error') {
if (arg instanceof Error) {
throw arg;
} else {
throw new Error(arg);
}
}
}
return this;
}
///# @name stamp
///# @description This will generate a colorized timestamp and message depending on the time that's passed.
///# @arg {string} type ['log'] - This determins the type of stamp to return. This can be any of the types that you can pass to `this.log`
///# @returns {string} The stamp that was generated.
}, {
key: 'stamp',
value: function stamp() {
var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'log';
var now = new Date();
var timestamp = [now.getHours(), now.getMinutes(), now.getSeconds()].join(':');
var color = this.log_types[type];
var stamp = this.options.timestamp ? '[' + _chalk2.default.magenta(timestamp) + '] ' : '';
if (_logSymbols2.default[type]) {
stamp += _logSymbols2.default[type] + ' ';
}
if (['error', 'warning', 'info'].indexOf(type) !== -1) {
stamp += _chalk2.default[color](type) + ': ';
}
return stamp;
}
///# @name time
///# @description
///# This does the same thing as `console.time`.
///# @arg {string} label - This is the label for your timed event
///# @chainable
}, {
key: 'time',
value: function time(label) {
if (!label) {
return this.log('error', 'You must pass in a label for `Logger.prototype.time`');
}
_perfy2.default.start(label);
return this;
}
///# @name timeEnd
///# @description
///# This does the same thing as `console.timeEnd`.
///# @arg {string} label - This must be the same label that was passed to the associated `this.time` function
///# @returns {string} - The total time it took to run the process
}, {
key: 'timeEnd',
value: function timeEnd(label) {
if (!label) {
return this.log('error', 'You must pass in a label for `Logger.prototype.timeEnd`');
}
var time = '+' + (0, _formatSeconds2.default)(_perfy2.default.end(label).time);
return '' + _chalk2.default.cyan(time);
}
///# @name spinner
///# @description
///# This creates an instance of a spinner to help show progress of something. It returns and instance of [ora](https://github.com/sindresorhus/ora)
///# @arg {string} options - Same options passed to [ora](https://github.com/sindresorhus/ora). You can additionaly pass in `verbose` as an option that is a boolean
///# @returns {object} - The new instance of [ora](https://github.com/sindresorhus/ora)
}, {
key: 'spinner',
value: function spinner(options) {
if (typeof options === 'string') {
options = { text: options };
}
if (this.spinners[options.text]) {
return this.spinners[options.text];
}
var spinner = this.spinners[options.text] = (0, _ora2.default)(options);
spinner.title = options.text;
var self = this;
// store the originals
spinner.originalStart = spinner.start;
spinner.originalStop = spinner.stop;
// overwrite it to support timing
spinner.start = function start() {
if (!this.enabled || !self.options.spinners) {
return this;
}
this.originalStart();
if (self.options.verbose) {
self.time('spinner_' + this.title);
}
return this;
};
spinner.stop = function stop() {
if (!this.enabled || this.id == null || !self.options.spinners) {
return this;
}
if (self.options.verbose) {
var time = self.timeEnd('spinner_' + this.title);
spinner.text += ' ' + time;
this.succeed();
return this;
}
return this.originalStop();
};
spinner.fail = function fail(err) {
spinner.stopAndPersist(_logSymbols2.default.error);
// stop the rest of spinners
_toJs2.default.each(self.spinners, function (_ref) {
var value = _ref.value;
value.originalStop();
});
self.log('error', err);
};
spinner.stopAndPersist = function stopAndPersist(symbol) {
if (!this.enabled || this.id == null || !self.options.spinners) {
return this;
}
this.originalStop();
this.stream.write((symbol || ' ') + ' ' + this.text + '\n');
return this;
};
return spinner;
}
}]);
return Logger;
}(_eventsAsync2.default);
exports.default = Logger;