consola.deac7d5a.cjs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. const node_util = require('node:util');
  3. const node_path = require('node:path');
  4. function parseStack(stack) {
  5. const cwd = process.cwd() + node_path.sep;
  6. const lines = stack.split("\n").splice(1).map((l) => l.trim().replace("file://", "").replace(cwd, ""));
  7. return lines;
  8. }
  9. function writeStream(data, stream) {
  10. const write = stream.__write || stream.write;
  11. return write.call(stream, data);
  12. }
  13. const bracket = (x) => x ? `[${x}]` : "";
  14. class BasicReporter {
  15. formatStack(stack, opts) {
  16. return " " + parseStack(stack).join("\n ");
  17. }
  18. formatArgs(args, opts) {
  19. const _args = args.map((arg) => {
  20. if (arg && typeof arg.stack === "string") {
  21. return arg.message + "\n" + this.formatStack(arg.stack, opts);
  22. }
  23. return arg;
  24. });
  25. return node_util.formatWithOptions(opts, ..._args);
  26. }
  27. formatDate(date, opts) {
  28. return opts.date ? date.toLocaleTimeString() : "";
  29. }
  30. filterAndJoin(arr) {
  31. return arr.filter(Boolean).join(" ");
  32. }
  33. formatLogObj(logObj, opts) {
  34. const message = this.formatArgs(logObj.args, opts);
  35. if (logObj.type === "box") {
  36. return "\n" + [
  37. bracket(logObj.tag),
  38. logObj.title && logObj.title,
  39. ...message.split("\n")
  40. ].filter(Boolean).map((l) => " > " + l).join("\n") + "\n";
  41. }
  42. return this.filterAndJoin([
  43. bracket(logObj.type),
  44. bracket(logObj.tag),
  45. message
  46. ]);
  47. }
  48. log(logObj, ctx) {
  49. const line = this.formatLogObj(logObj, {
  50. columns: ctx.options.stdout.columns || 0,
  51. ...ctx.options.formatOptions
  52. });
  53. return writeStream(
  54. line + "\n",
  55. logObj.level < 2 ? ctx.options.stderr || process.stderr : ctx.options.stdout || process.stdout
  56. );
  57. }
  58. }
  59. exports.BasicReporter = BasicReporter;
  60. exports.parseStack = parseStack;