1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const fs = require('fs');
|
12 |
|
13 |
|
14 |
|
15 | const waitForAllCoverage = dirs => {
|
16 | const maxTries = 10000;
|
17 | let numTries = 0;
|
18 | return Promise.all(
|
19 | dirs.map(
|
20 | dir =>
|
21 | new Promise((resolve, reject) => {
|
22 | const interval = setInterval(() => {
|
23 | const coverageExists = fs.existsSync(
|
24 | `${dir}/coverage/coverage-final.json`
|
25 | );
|
26 | numTries += 1;
|
27 | if (coverageExists) {
|
28 | clearInterval(interval);
|
29 | resolve();
|
30 | } else if (numTries > maxTries) {
|
31 | clearInterval(interval);
|
32 | reject();
|
33 | }
|
34 | }, 100);
|
35 | })
|
36 | )
|
37 | );
|
38 | };
|
39 |
|
40 | module.exports = function(rootDir /*: string */) {
|
41 | return waitForAllCoverage([rootDir]).then(() => {
|
42 | const createReporter = require('istanbul-api').createReporter;
|
43 | const istanbulCoverage = require('istanbul-lib-coverage');
|
44 |
|
45 | const map = istanbulCoverage.createCoverageMap();
|
46 | const reporter = createReporter();
|
47 |
|
48 | [rootDir].forEach(dir => {
|
49 |
|
50 | const coverage = require(`${dir}/coverage/coverage-final.json`);
|
51 | Object.keys(coverage).forEach(filename => {
|
52 | const obj = coverage[filename];
|
53 |
|
54 |
|
55 | map.addFileCoverage(obj.data ? obj.data : obj);
|
56 | });
|
57 | });
|
58 |
|
59 | reporter.dir = `${rootDir}/coverage`;
|
60 | reporter.addAll(['json', 'lcov', 'text', 'cobertura']);
|
61 | reporter.write(map);
|
62 | });
|
63 | };
|