@drainpixie/pika
Version:
a cute and shiny logger
6 lines • 3.29 kB
JavaScript
import{parse as E}from"node:path";var a={TICK:"\u2714",INFO:"\u2139",CROSS:"\u2716",WARNING:"\u26A0",ELLIPSIS:"\u2026",POINTER_SMALL:"\u203A"},o={RED:"\x1B[31m",BOLD:"\x1B[1m",GREY:"\x1B[90m",BLUE:"\x1B[34m",CYAN:"\x1B[36m",RESET:"\x1B[0m",GREEN:"\x1B[32m",YELLOW:"\x1B[33m",MAGENTA:"\x1B[35m",UNDERLINE:"\x1B[4m"},i={TRACE:0,DEBUG:1,INFO:2,WARN:3,ERROR:4,FATAL:5},u=(n,e,t,r,c=process.stdout)=>Object.freeze({label:n,level:e,badge:t,colour:r,stream:c}),y=(n,...e)=>`${e.join("")}${n}${o.RESET}`;function f(){return process.env.NO_COLOR||process.env.NODE_DISABLE_COLORS?!1:process.env.FORCE_COLOR?!0:process.stdout.isTTY??!1}var l={success:u("success",i.INFO,a.TICK,o.GREEN),warn:u("warn",i.WARN,a.WARNING,o.YELLOW),error:u("error",i.ERROR,a.CROSS,o.RED,process.stderr),fatal:u("fatal",i.FATAL,a.CROSS,o.RED,process.stderr),trace:u("trace",i.TRACE,a.ELLIPSIS,o.BLUE),debug:u("debug",i.DEBUG,a.INFO,o.CYAN),info:u("info",i.INFO,a.INFO,o.BLUE)};import{fileURLToPath as m}from"node:url";var d=class n{#o;#s;#a;#n;#r;constructor(e={}){this.#o=e.scope??this.#i(),this.#s=e.level??i.INFO,this.#a=this.#l(),this.#r=Object.freeze([...e.secrets??[]]),this.#n=e.useColors??f()}scope(e){return new n({...this.options,scope:e})}level(e){return new n({...this.options,level:e})}secrets(...e){return new n({...this.options,secrets:[...this.#r,...e]})}get options(){return{scope:this.#o,level:this.#s,secrets:this.#r,useColors:this.#n}}#i(){let e=Error.prepareStackTrace;try{Error.prepareStackTrace=(s,h)=>h;let r=new Error().stack;if(!r?.length)return"anonymous";let c=r[0].getFileName(),p=r.slice(1).find(s=>s.getFileName()!==c);if(!p)return"anonymous";let g=p.getFileName();return g?E(g.startsWith("file://")?m(g):g).name:"anonymous"}catch{return"anonymous"}finally{Error.prepareStackTrace=e}}#l(){return Math.max(...Object.values(l).map(e=>e.label?.length??0).filter(e=>e>0),0)}#c(e){return this.#r.length===0?e:this.#r.reduce((t,r)=>t.replaceAll(r,"[REDACTED]"),e)}#p(){return`[${this.#o}]`}#u(){let e=[this.#p()];return e.length>0?(e.push(a.POINTER_SMALL),e.map(t=>y(t,o.GREY))):e}#t(e,...t){return this.#n?y(e,...t):e}#g(e){if(!e.stack)return e.message;let[t,...r]=e.stack.split(`
`),c=this.#t(t,o.UNDERLINE),p=this.#t(r.join(`
`),o.GREY);return`${c}
${p}`}#h(e){return Object.entries(e).map(([t,r])=>`${t} = ${JSON.stringify(r)}`).join(", ")}#y(e,...t){let r=[...this.#u()],{label:c,colour:p,badge:g}=e;if(r.push(this.#t(`${g} `,p)),r.push(`${this.#t(c,p,o.UNDERLINE)}${" ".repeat(Math.max(0,this.#a-c.length))}`),t.length===0)return r.join(" ");let[s,...h]=t;return s instanceof Error?r.push(this.#g(s)):typeof s=="object"&&s!==null&&!(s instanceof Date)&&!(s instanceof RegExp)?r.push(this.#h(s)):r.push(this.#t([s,...h].map(L=>String(L)).join(" "),o.GREY)),r.join(" ")}#e(e,...t){e.level<this.#s||e.stream.write(`${this.#c(this.#y(e,...t))}
`)}clone(e={}){return new n({...this.options,...e})}success=(...e)=>this.#e(l.success,...e);error=(...e)=>this.#e(l.error,...e);fatal=(...e)=>this.#e(l.fatal,...e);trace=(...e)=>this.#e(l.trace,...e);debug=(...e)=>this.#e(l.debug,...e);warn=(...e)=>this.#e(l.warn,...e);info=(...e)=>this.#e(l.info,...e)},b=(n={})=>new d(n);export{o as Colour,a as Figure,i as Level,d as Pika,y as colour,b as pika,f as shouldUseColors};
//# sourceMappingURL=index.js.map