@@ -5,6 +5,13 @@ const debug = require('debug')('check-code-coverage')
5
5
const path = require ( 'path' )
6
6
const fs = require ( 'fs' )
7
7
const os = require ( 'os' )
8
+ const arg = require ( 'arg' )
9
+
10
+ const args = arg ( {
11
+ '--from' : String , // input json-summary filename, by default "coverage/coverage-summary.json"
12
+ '--set' : String // so we can convert "78%" into numbers ourselves
13
+ } )
14
+ debug ( 'args: %o' , args )
8
15
9
16
const availableColors = [ 'red' , 'yellow' , 'green' , 'brightgreen' ]
10
17
@@ -23,10 +30,30 @@ function getColor(coveredPercent) {
23
30
return 'brightgreen'
24
31
}
25
32
26
- function updateBadge ( ) {
27
- const coverageFilename = path . join ( process . cwd ( ) , 'coverage' , 'coverage-summary.json' )
28
- const coverage = require ( coverageFilename )
29
- const pct = coverage . total . statements . pct
33
+ function readCoverage ( filename ) {
34
+ if ( ! filename ) {
35
+ filename = path . join ( process . cwd ( ) , 'coverage' , 'coverage-summary.json' )
36
+ }
37
+ debug ( 'reading coverage summary from: %s' , filename )
38
+ const coverage = require ( filename )
39
+ return coverage . total . statements . pct
40
+ }
41
+
42
+ function updateBadge ( args ) {
43
+ let pct = 0
44
+ if ( args [ '--set' ] ) {
45
+ // make sure we can handle "--set 70" and "--set 70%"
46
+ pct = parseFloat ( args [ '--set' ] )
47
+ debug ( 'using coverage number: %d' , pct )
48
+ } else {
49
+ pct = readCoverage ( args [ '--from' ] )
50
+ }
51
+ if ( pct < 0 ) {
52
+ pct = 0
53
+ } else if ( pct > 100 ) {
54
+ pct = 100
55
+ }
56
+ debug ( 'clamped coverage: %d' , pct )
30
57
31
58
const readmeFilename = path . join ( process . cwd ( ) , 'README.md' )
32
59
const readmeText = fs . readFileSync ( readmeFilename , 'utf8' )
@@ -85,4 +112,4 @@ function updateBadge() {
85
112
}
86
113
}
87
114
88
- updateBadge ( )
115
+ updateBadge ( args )
0 commit comments