All files badges.ts

100% Statements 27/27
93.75% Branches 30/32
100% Functions 3/3
100% Lines 27/27

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                                                1x 1x         1x                 13x 13x 13x   13x 10x     13x 1x   12x 12x 36x 1x   35x 10x   25x     12x 12x               12x 1x     12x 1x 1x   1x     12x 12x    
import { badgen } from 'badgen';
import { readFileSync } from 'fs';
import svgToTinyDataUri from 'mini-svg-data-uri';
import get from 'lodash.get';
 
// Copied from `badgen` because it's not exported
export type StyleOption = 'flat' | 'classic';
 
export interface BadgenOptions {
  status: string;
  subject?: string;
  color?: string;
  label?: string;
  style?: StyleOption;
  jsonPath?: string;
  labelColor?: string;
  icon?: string;
  iconWidth?: number;
  scale?: number;
  arbitrary?: boolean;
}
 
export interface BadgeOption extends BadgenOptions {}
 
const getIconString = (path: string) => {
  return readFileSync(path, 'utf8');
}
 
type ColorData = Record<string, number[]>;
 
const defaultColorData: ColorData = {
  '#49c31a': [100],
  '#97c40f': [99.99, 90],
  '#a0a127': [89.99, 80],
  '#cba317': [79.99, 60],
  '#ce0000': [59.99, 0],
}
 
export function badge(option: BadgeOption, summary: object) {
  const { label = 'coverage', style = 'classic', jsonPath = 'total.statements.pct', color: optionColor, arbitrary, ...otherOption } = (option || {}) as BadgenOptions
  let pct: any = summary;
  pct = get(summary, jsonPath, 0);
 
  if (!arbitrary && !isNaN(Number(pct))) {
    pct = Number(pct);
  }
 
  if (!arbitrary && typeof pct !== 'number') {
    throw new Error(`${jsonPath} evaluates to ${JSON.stringify(pct)} and is not a suitable path in the JSON coverage data`);
  }
  const colorData = defaultColorData
  const color = Object.keys(colorData).find((value: keyof typeof colorData, idx) => {
    if (colorData[value].length === 1 && pct >= colorData[value][0]) {
      return true
    }
    if (colorData[value].length === 2 && pct <= colorData[value][0] && pct >= colorData[value][1]) {
      return true
    }
    return false;
  });
 
  const suffix = arbitrary ? "" : "%"
  const badgenArgs: BadgenOptions = {
    ...otherOption,
    style,
    label,
    status: `${pct < 0 ? 'Unknown' : `${pct}${suffix}`}`,
    color: (color || 'e5e5e5').replace(/^#/, ''),
  };
 
  if (optionColor) {
    badgenArgs.color = optionColor.replace(/^#/, '');
  }
 
  if(option.icon) {
    const svgString = getIconString(option.icon) as string;
    const svgDataUri = svgToTinyDataUri(svgString);
 
    badgenArgs.icon = svgDataUri;
  }
 
  console.log("badgenArgs", badgenArgs)
  return badgen(badgenArgs);
}