All files logger.ts

89.47% Statements 34/38
66.66% Branches 28/42
81.25% Functions 13/16
91.89% Lines 34/37

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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138        2x                                   2x                                   23x       3x                             18x   4x     5x   2x   2x   1x   1x       3x         11x 11x 11x 11x 11x                           2x 3x 2x 2x   2x 1x         12x 1x   11x 11x 12x 12x           6x   9x     9x           2x     2x  
import chalk from 'chalk';
import logSymbols from 'log-symbols';
 
// Icons for commands
export const icons: Record<string, string> = {
  goto: '→',
  click: '◉',
  type: '⌨',
  query: '?',
  screenshot: '📷',
  url: '🔗',
  html: '<>',
  back: '←',
  forward: '→',
  reload: '↻',
  wait: '⏳',
  newpage: '+',
  close: '✕',
  eval: '⚡',
};
 
// Colors for command types
export const cmdColor: Record<string, (s: string) => string> = {
  goto: chalk.cyan,
  click: chalk.yellow,
  type: chalk.magenta,
  query: chalk.blue,
  screenshot: chalk.green,
  url: chalk.cyan,
  html: chalk.blue,
  back: chalk.yellow,
  forward: chalk.yellow,
  reload: chalk.yellow,
  wait: chalk.gray,
  newpage: chalk.green,
  close: chalk.red,
  eval: chalk.magenta,
};
 
export function ts(): string {
  return chalk.gray(`[${new Date().toISOString()}]`);
}
 
export function truncate(str: string, max: number): string {
  return str.length > max ? `${str.slice(0, max)}...` : str;
}
 
export interface CommandLike {
  cmd: string;
  url?: string;
  selector?: string;
  text?: string;
  path?: string;
  full?: boolean;
  ms?: number;
  script?: string;
}
 
export function getCommandDetail(cmd: CommandLike): string | undefined {
  switch (cmd.cmd) {
    case 'goto':
      return chalk.white(cmd.url);
    case 'click':
    case 'query':
      return chalk.white(cmd.selector);
    case 'type':
      return `${chalk.white(cmd.selector)} ${chalk.dim(`="${cmd.text}"`)}`;
    case 'screenshot':
      return chalk.dim(cmd.path || 'screenshot.png');
    case 'html':
      return cmd.full ? chalk.dim('(full)') : undefined;
    case 'wait':
      return chalk.dim(`${cmd.ms || 1000}ms`);
    case 'eval':
      return chalk.dim(truncate(cmd.script || '', 50));
    default:
      return undefined;
  }
}
 
export function formatCommand(cmd: CommandLike): string {
  const color = cmdColor[cmd.cmd] || chalk.white;
  const icon = icons[cmd.cmd] || '•';
  const detail = getCommandDetail(cmd);
  const suffix = detail ? ` ${detail}` : '';
  return `${ts()} ${chalk.bold(color(icon))} ${color(cmd.cmd.toUpperCase())}${suffix}`;
}
 
export interface ResultLike {
  ok: boolean;
  error?: string;
  title?: string;
  url?: string;
  count?: number;
  path?: string;
  html?: string;
  result?: unknown;
}
 
const resultFormatters: Record<string, (r: ResultLike) => string | undefined> = {
  goto: (r) => r.title,
  click: (r) => (r.url ? `→ ${r.url}` : undefined),
  query: (r) => (r.count !== undefined ? `Found ${r.count} element(s)` : undefined),
  screenshot: (r) => (r.path ? `Saved to ${r.path}` : undefined),
  url: (r) => r.url,
  html: (r) => (r.html !== undefined ? `${r.html.length} chars` : undefined),
  eval: (r) => (r.result !== undefined ? truncate(JSON.stringify(r.result), 80) : undefined),
};
 
export function formatResult(cmd: CommandLike, result: ResultLike): string {
  if (!result.ok) {
    return `${ts()}   ${logSymbols.error} ${chalk.red(result.error)}`;
  }
  const formatter = resultFormatters[cmd.cmd];
  const msg = formatter ? formatter(result) : undefined;
  const suffix = msg ? ` ${chalk.dim(msg)}` : '';
  return `${ts()}   ${logSymbols.success}${suffix}`;
}
 
export type LogFn = (msg: string) => void;
 
export function createLogger(logFn: LogFn = console.log) {
  return {
    command(cmd: CommandLike): void {
      logFn(formatCommand(cmd));
    },
    result(cmd: CommandLike, result: ResultLike): void {
      logFn(formatResult(cmd, result));
    },
  };
}
 
// Default logger to stdout
export const logger = createLogger();
 
// Logger to stderr (for MCP)
export const stderrLogger = createLogger((msg) => process.stderr.write(`${msg}\n`));