forked from pamepeixinho/jest-coverage-badges
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
executable file
·55 lines (42 loc) · 1.24 KB
/
cli.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
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
#!/usr/bin/env node
const { get } = require('https')
const { readFile, writeFile } = require('fs')
const getColour = coverage => {
if (coverage < 80) {
return 'red';
}
if (coverage < 90) {
return 'yellow';
}
return 'brightgreen';
}
const reportKeys = ['lines', 'statements', 'functions', 'branches'];
const getBadge = (report, key) => {
if (!(report && report.total && report.total[key])) {
throw new Error('malformed coverage report');
}
const coverage = report.total[key].pct;
const colour = getColour(coverage);
return `https://img.shields.io/badge/Coverage-${coverage}${encodeURI('%')}-${colour}.svg`;
}
const download = (url, cb) => {
get(url, res => {
let file = '';
res.on('data', chunk => (file += chunk));
res.on('end', () => cb(null, file));
}).on('error', err => cb(err));
};
const getBadgeByKey = (report) => (key) => {
const url = getBadge(report, key);
download(url, (err, res) => {
if (err) throw err
writeFile(`./coverage/badge-${key}.svg`, res, 'utf8', err => {
if (err) throw err
});
});
};
readFile('./coverage/coverage-summary.json', 'utf8', (err, res) => {
if (err) throw err
const report = JSON.parse(res);
reportKeys.forEach(getBadgeByKey(report));
});