All files / src/utils/parsers index.ts

100% Statements 20/20
100% Branches 12/12
100% Functions 2/2
100% Lines 19/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 46 47 48 49              1x     5x   5x 14x   14x     4x     1x       11x 2x 9x 2x 7x 2x 5x 2x 3x 2x     1x     1x                
import { readFile } from 'fs/promises';
import { lcov } from './lcov';
import { json } from './json';
import { clover } from './clover';
import { cobertura } from './cobertura';
import type { Results } from './base';
 
export const parsers = [lcov, json, clover, cobertura] as const;
 
export async function coverage(path: string): Promise<Results> {
  const contents = await readFile(path, 'utf-8');
 
  for (const parser of parsers) {
    const result = parser.matches(path, contents);
 
    if (result === false) continue;
 
    // @ts-expect-error the result will always be what the parser desires, but typescript is confused :<
    return parser.parse(result);
  }
 
  throw new Error(`Failed to parse. ${path}`);
}
 
export function color(percentage: number): string {
  if (percentage > 97) {
    return colors.brightGreen;
  } else if (percentage > 93) {
    return colors.green;
  } else if (percentage > 90) {
    return colors.yellowGreen;
  } else if (percentage > 85) {
    return colors.yellow;
  } else if (percentage > 75) {
    return colors.orange;
  }
 
  return colors.red;
}
 
export const colors = {
  brightGreen: '#4C1',
  green: '#97CA00',
  yellowGreen: '#A4A61D',
  yellow: '#DFB317',
  orange: '#FE7D37',
  red: '#E05D44',
};