-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-build.js
41 lines (34 loc) · 1.54 KB
/
post-build.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
import "./correct-import-extensions.js";
import { promises as fs } from "fs";
if (!process.env.NO_COVERAGE_BADGE_UPDATE) {
const badges = [];
badges.push(
`![nycrc config on GitHub](https://img.shields.io/nycrc/virtualstate/x)`
)
const coverage = await fs.readFile("coverage/coverage-summary.json", "utf8").then(JSON.parse).catch(() => ({}));
const coverageConfig = await fs.readFile(".nycrc", "utf8").then(JSON.parse);
for (const [name, { pct }] of Object.entries(coverage?.total ?? {})) {
if (name.includes("/")) continue; // It is a file
const good = coverageConfig[name] ?? 80;
const color = pct >= good ? "brightgreen" : "yellow";
const message = `${pct}%25`;
badges.push(
`![${message} ${name} covered](https://img.shields.io/badge/${name}-${message}-${color})`
);
}
const tag = "[//]: # (badges)";
const readMe = await fs.readFile("README.md", "utf8");
const badgeStart = readMe.indexOf(tag);
const badgeStartAfter = badgeStart + tag.length;
if (badgeStart === -1) {
throw new Error(`Expected to find "${tag}" in README.md`);
}
const badgeEnd = badgeStartAfter + readMe.slice(badgeStartAfter).indexOf(tag);
const badgeEndAfter = badgeEnd + tag.length;
const readMeBefore = readMe.slice(0, badgeStart);
const readMeAfter = readMe.slice(badgeEndAfter);
const readMeNext = `${readMeBefore}${tag}\n\n${badges.join(" ")}\n\n${tag}${readMeAfter}`;
await fs.writeFile("README.md", readMeNext);
await fs.writeFile("packages/x/README.md", readMeNext);
console.log("Wrote coverage badges!");
}