-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
cli.js
executable file
·77 lines (66 loc) · 1.79 KB
/
cli.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env node
const { get } = require('https')
const { readFile, writeFile } = require('fs')
const { basename } = require('path')
const mri = require('mri')
const getColour = (coverage) => {
if (coverage < 80) {
return 'red'
}
if (coverage < 90) {
return 'yellow'
}
return 'brightgreen'
}
const getBadge = (report) => {
if (!(report && report.total && report.total.statements)) {
throw new Error('malformed coverage report')
}
const coverage = report.total.statements.pct
const colour = getColour(coverage)
return `https://img.shields.io/badge/Coverage-${coverage}${encodeURI(
'%'
)}-${colour}.svg`
}
const download = (url, cb) => {
get(url, (res) => {
let file = ''
if (res.statusCode > 299) {
cb(new Error(`${res.statusCode}: ${res.statusMessage}`), file)
}
res.on('data', (chunk) => (file += chunk))
res.on('end', () => cb(null, file))
}).on('error', (err) => cb(err))
}
const options = {
alias: {
h: 'help',
outputPath: 'output-path',
},
boolean: 'help',
default: {
'output-path': './coverage/badge.svg',
'report-path': './coverage/coverage-summary.json',
},
}
const [, filename, ...args] = process.argv
const { _, help, ...params } = mri(args, options) // eslint-disable-line no-unused-vars
if (help) {
console.log(
`usage: ${basename(filename)} [-h,--help] [--report-path] [--output-path]`
)
process.exit()
}
const { outputPath, 'report-path': reportPath } = params
readFile(reportPath, 'utf8', (err, res) => {
if (err) throw err
const report = JSON.parse(res)
const url = getBadge(report)
download(url, (err, res) => {
if (err) throw err
writeFile(outputPath, res, 'utf8', (err) => {
if (err) throw err
console.log('Wrote coverage badge to: ' + outputPath)
})
})
})