forked from romeovs/lcov-reporter-action
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbadge.js
More file actions
49 lines (47 loc) · 1.57 KB
/
badge.js
File metadata and controls
49 lines (47 loc) · 1.57 KB
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
import path from "path";
import fs from "fs";
import { badgen } from "badgen";
import { percentage } from "./lcov";
const badge = (option, pct) => {
const { label = "coverage", style = "classic" } = option || {};
const colorData = {
"49c31a": [100],
"97c40f": [99.99, 90],
a0a127: [89.99, 80],
cba317: [79.99, 60],
ce0000: [59.99, 0],
};
const color = Object.keys(colorData).find(
(value) =>
(colorData[value].length === 1 && pct >= colorData[value][0]) ||
(colorData[value].length === 2 &&
pct <= colorData[value][0] &&
pct >= colorData[value][1]),
);
const badgenArgs = {
style,
label,
status: `${pct < 0 ? "Unknown" : `${pct}%`}`,
color: color || "e5e5e5",
};
return badgen(badgenArgs);
};
export const createBadges = (badgePath, toCheck, options) => {
const dirName = path.resolve(badgePath);
if (!fs.existsSync(dirName)) {
fs.mkdirSync(dirName);
} else if (!fs.statSync(dirName).isDirectory()) {
throw new Error("badge path is not a directory");
}
for (const lcovObj of toCheck) {
const coverage = percentage(lcovObj.lcov);
const svgStr = badge(options, coverage.toFixed(2));
const fileName = path.join(dirName, `${lcovObj.packageName}.svg`);
console.log("writing badge", fileName);
try {
fs.writeFileSync(fileName, svgStr);
} catch (err) {
console.error("Error writing badge", fileName, err.toString());
}
}
};