/
index.js
46 lines (38 loc) · 1.15 KB
/
index.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
'use strict'
const fs = require('fs')
const pumpify = require('pumpify')
const promiseify = require('pify')
const through = require('through2')
const parse = require('./parse')
module.exports = tapeIstanbul
function tapeIstanbul (output) {
// Create a pumpify pipeline populated in the scope below
const pause = pumpify()
// Use a promise to track the write of the coverage file
// Only end the stream when it resolves
// Promises are a decent way to accomplish this without much code
const p = new Promise(function (resolve, reject) {
pause.setPipeline(
parse().on('coverage', writeFile),
through(passThrough, flush)
)
function writeFile (coverage) {
promiseify(fs.writeFile)(output || 'coverage.json', JSON.stringify(coverage, null, 2))
.then(function (results) {
pause.emit('end')
resolve(results)
})
.catch(function (err) {
pause.emit('error', err)
reject(err)
})
}
})
return pause
function flush (callback) {
p.then(callback).catch((err) => callback(err))
}
}
function passThrough (chunk, enc, callback) {
callback(null, chunk)
}