From e20a3d3747157a9c4830cd4454850f19aa2b2819 Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Tue, 25 Oct 2022 06:42:34 +0100 Subject: [PATCH] feat: add better error message when the `coverageFile` is not found --- src/index.ts | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 754ed11..1f7a907 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import yargs from "yargs"; -import type { Options } from "yargs"; +import fs from "fs"; +import type { Options, Arguments } from "yargs"; import { hideBin } from "yargs/helpers"; import exec from "./exec"; @@ -11,7 +12,7 @@ const parseDiff = require("./diff-parser"); const parseLcov = require("./lcov-parser"); export const error = (message: string) => { - console.error(message); + console.error("[ERROR] " + message); }; const options: { [key: string]: Options } = { @@ -22,8 +23,27 @@ const options: { [key: string]: Options } = { }, }; +type Argv = Arguments< + Partial<{ + coverageFile: string; + }> +>; + +export const validate = async (argv: Argv) => { + if (!fs.existsSync(argv.coverageFile || "")) { + return new Error( + `Lcov file must be a valid file '${argv.coverageFile}' provided. ` + + `Please ensure you have run tests with coverage enabled.` + ); + } +}; + export const run = async (argv = process.argv) => { - const parsed: any = yargs(hideBin(argv)).options(options).argv; + const parsed: Argv = await yargs(hideBin(argv)).options(options).argv; + const validationError = await validate(parsed); + if (validationError instanceof Error) { + return error(validationError.message); + } const diffText = await exec(`git diff origin/HEAD...HEAD`); if (diffText.code > 0) {