UNPKG

1.32 kBJavaScriptView Raw
1const {readFile, writeFile} = require('fs');
2
3const COVERAGE_SUMMARY_PATH = './coverage/coverage-summary.json';
4const OUTPUT_FILE = '../../TESTING.md';
5
6const reportKeys = ['lines', 'statements', 'functions', 'branches'];
7
8const getBadgeColor = (percentage) => {
9 if (percentage < 70) return 'red';
10 if (percentage < 85) return 'yellow';
11 return 'brightgreen';
12};
13
14const getBadges = (report) => {
15 const urls = {};
16 reportKeys.forEach((key) => {
17 const coverage = report.total[key].pct;
18 urls[key] = `https://img.shields.io/badge/Coverage_${key}-${coverage}${encodeURI(
19 '%'
20 )}-${getBadgeColor(coverage)}.svg`;
21 });
22 return urls;
23};
24
25const replaceOldBadges = (oldFileAsString, newBadges) => {
26 reportKeys.forEach((key) => {
27 const regex = `!\\[${key}\\]\\([^\\)]*\\)`;
28 oldFileAsString = oldFileAsString.replace(new RegExp(regex), `![${key}](${newBadges[key]})`);
29 })
30 return oldFileAsString
31}
32
33readFile(COVERAGE_SUMMARY_PATH, 'utf8', (errInput, resInput) => {
34 if (errInput) {
35 throw errInput;
36 }
37
38 const report = JSON.parse(resInput);
39 const badges = getBadges(report);
40
41 readFile(OUTPUT_FILE, 'utf8', (errOutput, resOutput) => {
42
43 const newFile = replaceOldBadges(resOutput, badges);
44
45 writeFile(OUTPUT_FILE, newFile, 'utf8', (writeError) => {
46 if (writeError) {
47 throw writeError;
48 }
49 });
50 });
51});