@@ -5,6 +5,13 @@ const debug = require('debug')('check-code-coverage')
55const path = require ( 'path' )
66const fs = require ( 'fs' )
77const 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 )
815
916const availableColors = [ 'red' , 'yellow' , 'green' , 'brightgreen' ]
1017
@@ -23,10 +30,30 @@ function getColor(coveredPercent) {
2330 return 'brightgreen'
2431}
2532
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 )
3057
3158 const readmeFilename = path . join ( process . cwd ( ) , 'README.md' )
3259 const readmeText = fs . readFileSync ( readmeFilename , 'utf8' )
@@ -85,4 +112,4 @@ function updateBadge() {
85112 }
86113}
87114
88- updateBadge ( )
115+ updateBadge ( args )
0 commit comments