All files / src/config inputs.ts

0% Statements 0/19
0% Branches 0/14
0% Functions 0/6
0% Lines 0/18

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                                                                                                                                                       
import { glob } from 'glob';
import * as core from '@actions/core';
import type { Format } from 'badge-maker';
import { dirname, join, resolve } from 'path';
import { parsers } from '../utils/parsers';
 
export type Inputs = {
  input: string;
  output: string;
  style: Format['style'];
  label: string;
};
 
export async function inputs(): Promise<Inputs> {
  const file = await input.file();
  const output = resolve(input.get('output', false, join(dirname(file), './badge.svg')));
 
  return {
    input: file,
    output,
    style: input.style('flat'),
    label: input.get('label', false, 'coverage'),
  };
}
 
export namespace input {
  export type Style = Format['style'];
 
  export async function file(): Promise<string> {
    let value = get('input');
 
    if (!value) {
      const globs = parsers.flatMap((parser) => parser.globs);
 
      const files = await glob(globs);
 
      value = files.shift();
    }
 
    if (!value) {
      throw new Error('No "input" was provided and we were unable to detect a coverage directory.');
    }
 
    return resolve(value);
  }
 
  export function style(defaultValue: Style): Style {
    const style = get('style') ?? defaultValue;
 
    if (is.style(style)) {
      return style;
    }
 
    console.warn(`Invalid style ${style} therefore we're falling back to the default style.`);
    return defaultValue;
  }
 
  export function get(key: string, required: boolean | undefined, defaultValue: string): string;
  export function get(key: string, required: true, defaultValue?: string): string;
  export function get(key: string, required?: boolean, defaultValue?: string): string | undefined;
  export function get(key: string, required?: boolean, defaultValue?: string): string | undefined {
    return (
      core.getInput(key, {
        required,
        trimWhitespace: true,
      }) || defaultValue
    );
  }
}
 
export namespace is {
  export function style(value: any): value is Format['style'] {
    return ['plastic', 'flat', 'flat-square', 'for-the-badge', 'social'].includes(value);
  }
}