UNPKG

hagen

Version:

A colorful logger for JS in Node and the Browser

2 lines (1 loc) 2.93 kB
"use strict";var f=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames,m=Object.getOwnPropertySymbols;var b=Object.prototype.hasOwnProperty,L=Object.prototype.propertyIsEnumerable;var u=(e,o,n)=>o in e?f(e,o,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[o]=n,c=(e,o)=>{for(var n in o||(o={}))b.call(o,n)&&u(e,n,o[n]);if(m)for(var n of m(o))L.call(o,n)&&u(e,n,o[n]);return e};var W=(e,o)=>{for(var n in o)f(e,n,{get:o[n],enumerable:!0})},E=(e,o,n,l)=>{if(o&&typeof o=="object"||typeof o=="function")for(let r of S(o))!b.call(e,r)&&r!==n&&f(e,r,{get:()=>o[r],enumerable:!(l=y(o,r))||l.enumerable});return e};var O=e=>E(f({},"__esModule",{value:!0}),e);var T={};W(T,{default:()=>M,error:()=>I,getConfig:()=>x,info:()=>v,log:()=>k,resetConfig:()=>B,setConfig:()=>P,success:()=>w,warn:()=>$});module.exports=O(T);var C=require("chalk"),h=require("std-env");var i=new C.Chalk({level:3});h.isCI&&(i.level=0);var p={showTimestamp:!1,colors:{reserved:{WARN:i.bgYellowBright.black,ERROR:i.bgRedBright.black,INFO:i.bgBlack.white,SUCCESS:i.bgBlack.greenBright},normal:[i.bgBlue.white,i.bgGreen.black,i.bgCyan.black,i.bgRed.white,i.bgMagenta.white,i.bgYellow.black]}},t=c({},p);function P(e){t=c(c({},t),e)}function x(){return t}function B(){t=c({},p)}function N(e){let o=Array.from(e).reduce((n,l)=>n+l.charCodeAt(0),0);return t.colors.normal[o%t.colors.normal.length]}function A(e,o,n="end"){if(e.length===o)return e;if(e.length<o){let a=o-e.length,s=Math.floor(a/2),R=a-s;return" ".repeat(s)+e+" ".repeat(R)}let l="\u2026",r=o-1;switch(n){case"start":return`${l}${e.slice(e.length-r)}`;case"end":return`${e.slice(0,r)}${l}`;case"middle":{let a=Math.ceil(r/2),s=Math.floor(r/2);return`${e.slice(0,a)}${l}${e.slice(e.length-s)}`}default:throw new Error(`Invalid truncation method: ${n}`)}}function g({logger:e,label:o,data:n}){let l,r="\u2022";if(typeof o=="object"?(r=o.label?o.label:r,"color"in o?typeof o.color=="number"?l=t.colors.normal[o.color]:l=o.color:l=i.bgHex(o.bgColor).hex(o.fgColor)):(r=o||r,l=N(o)),r=r.trim(),t.fixedWidth!==void 0&&(r=A(r,t.fixedWidth.width,t.fixedWidth.truncationMethod)),r=l.bold(` ${r} `),h.isCI&&(r=`[${r}]`),t.showTimestamp){let a=new Date().toISOString(),s=i.gray(`[ ${a} ]`);r=`${r} ${s}`}e(r,...n)}function d(e,o){return`${e} ${o}`.trim()}function k(e,...o){g({logger:console.log,label:e,data:o})}function v(e,...o){g({logger:console.log,label:{label:d("i",e),color:t.colors.reserved.INFO},data:o})}function w(e,...o){g({logger:console.log,label:{label:d("\u2713",e),color:t.colors.reserved.SUCCESS},data:o})}function $(e,...o){g({logger:console.warn,label:{label:d("!",e),color:t.colors.reserved.WARN},data:o})}function I(e,...o){g({logger:console.error,label:{label:d("\u2715",e),color:t.colors.reserved.ERROR},data:o})}var M={log:k,info:v,success:w,warn:$,error:I};0&&(module.exports={error,getConfig,info,log,resetConfig,setConfig,success,warn});