Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added --average flag #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const findArgument = (argName, defaultOutput) => {
return defaultOutput;
}

const index = process.argv.findIndex(a => a.match(argName))
const index = process.argv.findIndex(a => a.match(argName));
if (index < 0) {
return defaultOutput;
}
Expand Down Expand Up @@ -52,10 +52,37 @@ const getBadge = (report, key) => {
throw new Error('malformed coverage report');
}

const coverage = (!report.total[key] || typeof report.total[key].pct !== 'number') ? 0 : report.total[key].pct;
const colour = getColour(coverage);
coverage = (!report.total[key] || typeof report.total[key].pct !== 'number') ? 0 : report.total[key].pct;

return `https://img.shields.io/badge/Coverage${encodeURI(':')}${key}-${coverage}${encodeURI('%')}-${colour}.svg`;
return getBadgeUrl(coverage, key);
}

const getAverageBadge = (report, inputString) => {
let keys;
if (inputString === 'all') {
keys = reportKeys;
} else {
keys = inputString.split(',');
}

if (keys.length > 0) {
let coverages = keys.map(key => {
if (!(report && report.total && report.total[key])) {
throw new Error('malformed coverage report');
}

return (!report.total[key] || typeof report.total[key].pct !== 'number') ? 0 : report.total[key].pct;
});

let averageCoverage = coverages.reduce((a, b) => (a+b)) / coverages.length;

return getBadgeUrl(averageCoverage);
}
throw new Error('No keys given');
}

const getBadgeUrl = (coverage, key) => {
return `https://img.shields.io/badge/Coverage${key ? encodeURI(':')+key : ''}-${coverage}${encodeURI('%')}-${getColour(coverage)}.svg`;
}

const download = (url, cb) => {
Expand All @@ -68,17 +95,19 @@ const download = (url, cb) => {
}).on('error', err => cb(err));
}

const writeBadgeInFolder = (key, res) => {
writeFile(`${outputPath}/badge-${key}.svg`, res, 'utf8', (writeError) => {
const writeBadgeInFolder = (res, key) => {
writeFile(`${outputPath}/badge${key ? '-'+key : ''}.svg`, res, 'utf8', (writeError) => {
if (writeError) {
throw writeError;
}
});
}

const getBadgeByKey = report => (key) => {
const url = getBadge(report, key);
saveBadge(getBadge(report, key), key);
}

const saveBadge = (url, key) => {
download(url, (err, res) => {
if (err) {
throw err;
Expand All @@ -87,7 +116,7 @@ const getBadgeByKey = report => (key) => {
if (folderError) {
console.error(`Could not create output directory ${folderError}`);
} else {
writeBadgeInFolder(key, res);
writeBadgeInFolder(res, key);
}
})
})
Expand All @@ -100,4 +129,13 @@ readFile(`${inputPath}`, 'utf8', (err, res) => {

const report = JSON.parse(res);
reportKeys.forEach(getBadgeByKey(report));

let average = findArgument('average', '');

if (average === undefined && process.argv.filter(a => a.match('average'))) {
average = 'all';
}
if (average) {
saveBadge(getAverageBadge(report, average), 'average');
}
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-coverage-badges",
"version": "1.1.3",
"version": "1.2.0",
"description": "Create a group of coverage badges from jest",
"main": "cli.js",
"bin": "cli.js",
Expand Down