UNPKG

@scayle/storefront-nuxt

Version:

Nuxt integration for the SCAYLE Commerce Engine and Storefront API

60 lines (58 loc) 2.07 kB
import { formatWithOptions } from "node:util"; import { sep } from "node:path"; function parseStack(stack, message) { const cwd = process.cwd() + sep; const lines = stack.split("\n").splice(message.split("\n").length).map((l) => l.trim().replace("file://", "").replace(cwd, "")); return lines; } export class JSONReporter { formatStack(stack, message, opts) { const indent = " ".repeat((opts?.errorLevel || 0) + 1); return indent + parseStack(stack, message).join(` ${indent}`); } formatError(err, opts) { const message = "message" in err && err.message && typeof err.message === "string" ? err.message : formatWithOptions(opts, err); const stack = "stack" in err && err.stack && typeof err.stack === "string" ? this.formatStack(err.stack, message, opts) : ""; const level = opts?.errorLevel || 0; const causedPrefix = level > 0 ? `${" ".repeat(level)}[cause]: ` : ""; const causedError = "cause" in err && err.cause ? ` ${this.formatError(err.cause, { ...opts, errorLevel: level + 1 })}` : ""; return `${causedPrefix + message} ${stack}${causedError}`; } formatArgs(args, opts) { const _args = args.map((arg) => { if (arg && arg instanceof Error) { return this.formatError(arg, opts); } return arg; }); return formatWithOptions(opts, ..._args); } formatDate(date, opts) { return opts.date ? date.toLocaleTimeString() : ""; } filterAndJoin(arr) { return arr.filter(Boolean).join(" "); } formatLogObj(logObj, opts) { const message = this.formatArgs(logObj.args, opts); return JSON.stringify({ timestamp: logObj.date.toISOString(), message, severity: logObj.type }); } log(logObj, ctx) { const line = this.formatLogObj(logObj, { columns: ctx.options.stdout?.columns || 0, ...ctx.options.formatOptions, colors: false, compact: true }); const stream = logObj.level < 2 ? ctx.options.stderr || process.stderr : ctx.options.stdout || process.stdout; return stream.write(`${line} `); } }