Skip to content

Commit ec31b0c

Browse files
authored
feat: insert code coverage badge if not found (#11)
1 parent d09fde2 commit ec31b0c

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# check-code-coverage [![ci status][ci image]][ci url] ![mock coverage](https://img.shields.io/badge/code--coverage-100%-brightgreen)
1+
# check-code-coverage [![ci status][ci image]][ci url] ![check-code-coverage](https://img.shields.io/badge/code--coverage-100%-brightgreen)
22
> Utilities for checking the coverage produced by NYC against extra or missing files
33
44
## Use
@@ -34,7 +34,7 @@ check-total --from coverage/coverage-summary.json --min 80
3434

3535
If your README.md includes Shields.io badge, like this
3636

37-
![code coverage](https://img.shields.io/badge/code--coverage-80%-brightgreen)
37+
![check-code-coverage](https://img.shields.io/badge/code--coverage-80%-brightgreen)
3838

3939
You can update it using statements covered percentage from `coverage/coverage-summary.json` by running
4040

@@ -44,12 +44,14 @@ update-badge
4444

4545
If the coverage summary has 96%, then the above badge would be updated to
4646

47-
![code coverage](https://img.shields.io/badge/code--coverage-96%-brightgreen)
47+
![check-code-coverage](https://img.shields.io/badge/code--coverage-96%-brightgreen)
4848

4949
Related project: [dependency-version-badge](https://github.com/bahmutov/dependency-version-badge)
5050

5151
The badges will have different colors, depending on the coverage, see [bin/update-badge.js](bin/update-badge.js)
5252

53+
If the code coverage badge is not found, a new badge is inserted on the first line.
54+
5355
## Debug
5456

5557
To see verbose log messages, run with `DEBUG=check-code-coverage` environment variable

bin/update-badge.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const debug = require('debug')('check-code-coverage')
55
const path = require('path')
66
const fs = require('fs')
7+
const os = require('os')
78

89
const availableColors = ['red', 'yellow', 'green', 'brightgreen']
910

@@ -47,13 +48,31 @@ function updateBadge() {
4748
debug('coverage regex: "%s"', coverageRe)
4849
debug('new coverage badge: "%s"', coverageBadge)
4950

50-
const updatedReadmeText = readmeText.replace(
51+
let found
52+
let updatedReadmeText = readmeText.replace(
5153
coverageRe,
5254
(match) => {
55+
found = true
5356
debug('match: %o', match)
5457
return coverageBadge
5558
},
5659
)
60+
61+
if (!found) {
62+
console.log('⚠️ Could not find code coverage badge in file %s', readmeFilename)
63+
console.log('Insert new badge on the first line')
64+
// use NPM package name as label to flag where this badge is coming from
65+
const badge = `![check-code-coverage](${coverageBadge})`
66+
debug('inserting new badge: %s', badge)
67+
68+
const lines = readmeText.split(os.EOL)
69+
if (lines.length < 1) {
70+
console.error('File %s has no lines, cannot insert code coverage badge', readmeFilename)
71+
return readmeText
72+
}
73+
lines[0] += ' ' + badge
74+
updatedReadmeText = lines.join(os.EOL)
75+
}
5776
return updatedReadmeText
5877
}
5978

0 commit comments

Comments
 (0)