1 | const {readFile, writeFile} = require('fs');
|
2 |
|
3 | const COVERAGE_SUMMARY_PATH = './coverage/coverage-summary.json';
|
4 | const OUTPUT_FILE = '../../TESTING.md';
|
5 |
|
6 | const reportKeys = ['lines', 'statements', 'functions', 'branches'];
|
7 |
|
8 | const getBadgeColor = (percentage) => {
|
9 | if (percentage < 70) return 'red';
|
10 | if (percentage < 85) return 'yellow';
|
11 | return 'brightgreen';
|
12 | };
|
13 |
|
14 | const 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 |
|
25 | const 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 |
|
33 | readFile(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 | });
|