UNPKG

@eljs/utils

Version:
110 lines 4.56 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } import chalk from 'chalk'; import { EOL } from 'node:os'; import readline from 'readline'; import stripAnsi from 'strip-ansi'; export var prefixes = { event: chalk.magenta('event') + ' -', info: chalk.cyan('info') + ' -', warn: chalk.yellow('warn') + ' -', error: chalk.red('error') + ' -', fatal: chalk.red('fatal') + ' -', wait: chalk.cyan('wait') + ' -', ready: chalk.green('ready') + ' -' }; var Logger = /*#__PURE__*/function () { function Logger() { _classCallCheck(this, Logger); } _createClass(Logger, [{ key: "format", value: function format(label, message) { return message.split(EOL).map(function (line, i) { return i === 0 ? "".concat(label, " ").concat(line) : line.padStart(stripAnsi(label).length + line.length + 1); }).join(EOL); } }, { key: "log", value: function log(message) { var tag = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; tag ? console.log(this.format(this._chalkTag(tag), message)) : console.log(message); } }, { key: "event", value: function event(message) { console.log(this.format(prefixes.event, message)); } }, { key: "info", value: function info(message) { console.log(this.format(prefixes.info, message)); } }, { key: "warn", value: function warn(message) { console.warn(this.format(prefixes.warn, message)); } }, { key: "error", value: function error(message) { console.error(this.format(prefixes.error, message)); } }, { key: "fatal", value: function fatal(message) { console.error(this.format(prefixes.fatal, message)); } }, { key: "wait", value: function wait(message) { console.log(this.format(prefixes.wait, message)); } }, { key: "ready", value: function ready(message) { console.log(this.format(prefixes.ready, message)); } }, { key: "printErrorAndExit", value: function printErrorAndExit(message) { this.error(message); process.exit(1); } }, { key: "clear", value: function clear(title) { if (process.stdout.isTTY) { var blank = EOL.repeat(process.stdout.rows); console.log(blank); readline.cursorTo(process.stdout, 0, 0); readline.clearScreenDown(process.stdout); if (title) { console.log(title); } } } }, { key: "step", value: function step(name, message) { if (message) { console.log("".concat(EOL).concat(chalk.gray(">>> ".concat(name, ":")), " ").concat(chalk.magenta.bold(message))); } else { return function (message) { return console.log("".concat(EOL).concat(chalk.gray(">>> ".concat(name, ":")), " ").concat(chalk.magenta.bold(message))); }; } } }, { key: "_chalkTag", value: function _chalkTag(message) { return chalk.bgBlackBright.white.dim(" ".concat(message, " ")); } }]); return Logger; }(); export var logger = new Logger();