forked from ChatGPTBox-dev/chatGPTBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-coverage-badge.mjs
More file actions
41 lines (33 loc) · 1.24 KB
/
update-coverage-badge.mjs
File metadata and controls
41 lines (33 loc) · 1.24 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
import fs from 'node:fs'
import path from 'node:path'
const coverageSummaryPath = 'coverage/coverage-summary.json'
const badgePath = 'badges/coverage.json'
function getBadgeColor(percentage) {
if (percentage >= 90) return 'brightgreen'
if (percentage >= 80) return 'green'
if (percentage >= 70) return 'yellowgreen'
if (percentage >= 60) return 'yellow'
if (percentage >= 50) return 'orange'
return 'red'
}
function main() {
if (!fs.existsSync(coverageSummaryPath)) {
throw new Error(`Coverage summary file not found: ${coverageSummaryPath}`)
}
const summary = JSON.parse(fs.readFileSync(coverageSummaryPath, 'utf8'))
const linesPercentage = Number(summary?.total?.lines?.pct)
if (!Number.isFinite(linesPercentage)) {
throw new Error('Unable to read lines coverage percentage from coverage summary')
}
const roundedPercentage = Number(linesPercentage.toFixed(2))
const badge = {
schemaVersion: 1,
label: 'coverage',
message: `${roundedPercentage}%`,
color: getBadgeColor(roundedPercentage),
}
fs.mkdirSync(path.dirname(badgePath), { recursive: true })
fs.writeFileSync(badgePath, JSON.stringify(badge, null, 2) + '\n')
console.log(`Updated ${badgePath} with lines coverage ${roundedPercentage}%`)
}
main()