All files prettyPrint.ts

94.73% Statements 18/19
87.5% Branches 7/8
100% Functions 2/2
94.73% Lines 18/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 461x 1x 1x     1x               4x 1x 1x 3x 1x 1x   2x             1x 21x 4x 4x           17x   17x     2x        
import chalk from 'chalk';
import * as safeStringify from 'json-stringify-safe';
import { inspect } from 'util';
import { LogOutput } from './logger';
 
const inspectOptions: NodeJS.InspectOptions = {
  depth: 10,
  maxArrayLength: Infinity,
  compact: false,
  breakLength: 100,
};
 
function colorize (logOutput: LogOutput): string {
  if (logOutput.level === 'ERROR') {
    const inspected = inspect(logOutput, inspectOptions);
    return chalk.red(inspected);
  } else if (logOutput.level === 'WARN') {
    const inspected = inspect(logOutput, inspectOptions);
    return chalk.yellow(inspected);
  } else {
    return inspect(logOutput, {
      ...inspectOptions,
      colors: true // Add color, since output isn't in single color
    });
  }
}
 
export function prettyPrint(logOutput: LogOutput): string {
  if ('PN_LOG_PRETTY' in process.env) {
    Eif (chalk.supportsColor.hasBasic) {
      return colorize(logOutput);
    }
    // color unsupported, return regular inspect
    return inspect(logOutput, inspectOptions);
  } else {
    // Regular JSON output
    try {
      // Try using native stringify - fast
      return JSON.stringify(logOutput, null, 2);
    } catch {
      // Use safe parser if it throws - slower, but handles circular references
      return safeStringify(logOutput, null, 2);
    }
  }
}