| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
'use strict'; |
| 6 |
const { ReportBase } = require('istanbul-lib-report'); |
| 7 |
|
| 8 |
class JsonReport extends ReportBase { |
| 9 |
constructor(opts) { |
| 10 |
super(); |
| 11 |
|
| 12 |
this.file = opts.file || 'coverage-final.json'; |
| 13 |
this.first = true; |
| 14 |
} |
| 15 |
|
| 16 |
onStart(root, context) { |
| 17 |
this.contentWriter = context.writer.writeFile(this.file); |
| 18 |
this.contentWriter.write('{'); |
| 19 |
} |
| 20 |
|
| 21 |
onDetail(node) { |
| 22 |
const fc = node.getFileCoverage(); |
| 23 |
const key = fc.path; |
| 24 |
const cw = this.contentWriter; |
| 25 |
|
| 26 |
if (this.first) { |
| 27 |
this.first = false; |
| 28 |
} else { |
| 29 |
cw.write(','); |
| 30 |
} |
| 31 |
cw.write(JSON.stringify(key)); |
| 32 |
cw.write(': '); |
| 33 |
cw.write(JSON.stringify(fc)); |
| 34 |
cw.println(''); |
| 35 |
} |
| 36 |
|
| 37 |
onEnd() { |
| 38 |
const cw = this.contentWriter; |
| 39 |
cw.println('}'); |
| 40 |
cw.close(); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
module.exports = JsonReport; |