UNPKG

fakeit-facet

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.

316 lines (236 loc) 10.6 kB
"use strict"; var _Reflect$construct = require("@babel/runtime-corejs3/core-js-stable/reflect/construct"); var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); _Object$defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _includes = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/includes")); var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/keys")); var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass")); var _inherits2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/inherits")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/getPrototypeOf")); var _toJs = _interopRequireDefault(require("to-js")); var _chalk = _interopRequireDefault(require("chalk")); var _logSymbols = _interopRequireDefault(require("log-symbols")); var _perfy = _interopRequireDefault(require("perfy")); var _ora = _interopRequireDefault(require("ora")); var _formatSeconds = _interopRequireDefault(require("format-seconds")); var _eventsAsync = _interopRequireDefault(require("events-async")); var _lodash = require("lodash"); function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = _Reflect$construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !_Reflect$construct) return false; if (_Reflect$construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(_Reflect$construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } _logSymbols["default"].warn = _logSymbols["default"].warning; _logSymbols["default"].ok = _logSymbols["default"].okay = _logSymbols["default"].success; /// @name Logger /// @description /// This is the main logger for the application var Logger = /*#__PURE__*/function (_Emitter) { (0, _inherits2["default"])(Logger, _Emitter); var _super = _createSuper(Logger); /// # @name constructor /// # @arg {object} options [{ log: true, verbose: false, timestamp: true }] function Logger() { var _this; var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; (0, _classCallCheck2["default"])(this, Logger); _this = _super.call(this); _this.setMaxListeners(50); _this.options = _toJs["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, _createClass2["default"])(Logger, [{ key: "log", value: function log(type, arg) { if (type instanceof Error) { arg = type; type = 'error'; } if (this.options.log || type === 'error') { var _context, _context2; if ((0, _includes["default"])(_context = ['time', 'timeEnd']).call(_context, type)) { return this[type](arg); } if (!(0, _includes["default"])(_context2 = (0, _keys["default"])(_toJs["default"]).call(_toJs["default"], this.log_types)).call(_context2, type)) { 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 _context3; 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 ? "[".concat(_chalk["default"].magenta(timestamp), "] ") : ''; if (_logSymbols["default"][type]) { stamp += "".concat(_logSymbols["default"][type], " "); } if ((0, _includes["default"])(_context3 = ['error', 'warning', 'info']).call(_context3, type)) { stamp += "".concat(_chalk["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`'); } _perfy["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 = "+".concat((0, _formatSeconds["default"])(_perfy["default"].end(label).time)); return "".concat(_chalk["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]; } options.isEnabled = this.options.spinners || options.spinners || true; if (this.options.verbose) { options.stream = process.stdout; } var spinner = this.spinners[options.text] = (0, _ora["default"])(options); 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.isEnabled || !self.options.spinners) { return this; } this.originalStart(); if (self.options.verbose) { self.time("spinner_".concat(options.text)); } return this; }; spinner.stop = function stop() { if (!this.isEnabled || this.id == null || !self.options.spinners) { return this; } if (self.options.verbose) { var time = self.timeEnd("spinner_".concat(options.text)); spinner.text += " ".concat(time); this.succeed(); return this; } return this.originalStop(); }; spinner.fail = function fail(err) { spinner.stopAndPersist({ symbol: _logSymbols["default"].error }); // stop the rest of spinners _toJs["default"].each(self.spinners, function (_ref) { var value = _ref.value; value.originalStop(); }); self.log('error', err); }; spinner.stopAndPersist = function stopAndPersist(symbol) { var _context4; if (!this.isEnabled || this.id == null || !self.options.spinners) { return this; } this.originalStop(); var symbol_to_use = ' '; if (symbol) { symbol_to_use = symbol; if (!(0, _lodash.isEmpty)(symbol.symbol)) { symbol_to_use = symbol.symbol; } } this.stream.write((0, _concat["default"])(_context4 = "".concat(symbol_to_use, " ")).call(_context4, this.text, "\n")); return this; }; return spinner; } }]); return Logger; }(_eventsAsync["default"]); exports["default"] = Logger;