browser.cjs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. const core = require('./core.cjs');
  4. class BrowserReporter {
  5. constructor(options) {
  6. this.options = { ...options };
  7. this.defaultColor = "#7f8c8d";
  8. this.levelColorMap = {
  9. 0: "#c0392b",
  10. // Red
  11. 1: "#f39c12",
  12. // Yellow
  13. 3: "#00BCD4"
  14. // Cyan
  15. };
  16. this.typeColorMap = {
  17. success: "#2ecc71"
  18. // Green
  19. };
  20. }
  21. _getLogFn(level) {
  22. if (level < 1) {
  23. return console.__error || console.error;
  24. }
  25. if (level === 1) {
  26. return console.__warn || console.warn;
  27. }
  28. return console.__log || console.log;
  29. }
  30. log(logObj) {
  31. const consoleLogFn = this._getLogFn(logObj.level);
  32. const type = logObj.type === "log" ? "" : logObj.type;
  33. const tag = logObj.tag || "";
  34. const color = this.typeColorMap[logObj.type] || this.levelColorMap[logObj.level] || this.defaultColor;
  35. const style = `
  36. background: ${color};
  37. border-radius: 0.5em;
  38. color: white;
  39. font-weight: bold;
  40. padding: 2px 0.5em;
  41. `;
  42. const badge = `%c${[tag, type].filter(Boolean).join(":")}`;
  43. if (typeof logObj.args[0] === "string") {
  44. consoleLogFn(
  45. `${badge}%c ${logObj.args[0]}`,
  46. style,
  47. // Empty string as style resets to default console style
  48. "",
  49. ...logObj.args.slice(1)
  50. );
  51. } else {
  52. consoleLogFn(badge, style, ...logObj.args);
  53. }
  54. }
  55. }
  56. function createConsola(options = {}) {
  57. const consola2 = core.createConsola({
  58. reporters: options.reporters || [new BrowserReporter({})],
  59. prompt(message, options2 = {}) {
  60. if (options2.type === "confirm") {
  61. return Promise.resolve(confirm(message));
  62. }
  63. return Promise.resolve(prompt(message));
  64. },
  65. ...options
  66. });
  67. return consola2;
  68. }
  69. const consola = createConsola();
  70. exports.Consola = core.Consola;
  71. exports.LogLevels = core.LogLevels;
  72. exports.LogTypes = core.LogTypes;
  73. exports.consola = consola;
  74. exports.createConsola = createConsola;
  75. exports.default = consola;