-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbadge.js
28 lines (21 loc) · 915 Bytes
/
badge.js
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
const fetch = require('node-fetch');
const fs = require('fs');
const processBadgeColor = (coverage) => {
const colors = [
{ threshold: 50, color: 'red' },
{ threshold: 75, color: 'orange' },
{ threshold: 95, color: 'yellow' }
];
for (let i = 0 ; i < colors.length ; i++) {
if (coverage < colors[i].threshold) {
return colors[i].color;
}
}
return 'green';
}
const getBadgeUrl = (coverage, label) => `https://img.shields.io/static/v1?label=${encodeURIComponent(label)}&message=${encodeURIComponent(coverage)}%25&color=${processBadgeColor(coverage)}&style=for-the-badge`;
const generateBadge = async (coverageValue, label, badgeFilename, workingDir) => {
const badgeContent = await fetch(getBadgeUrl(coverageValue, label));
fs.writeFileSync(`${workingDir}/${badgeFilename}`, await badgeContent.text());
}
export { generateBadge };