feat: add better error message when the coverageFile is not found

This commit is contained in:
Ade Attwood 2022-10-25 06:42:34 +01:00
parent 8e9cbcd396
commit e20a3d3747

View file

@ -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) {