|
1 | 1 | // @ts-check
|
2 | 2 | const istanbul = require('istanbul-lib-coverage')
|
3 |
| -const { join, resolve } = require('path') |
| 3 | +const { join, resolve, isAbsolute } = require('path') |
4 | 4 | const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs')
|
5 | 5 | const execa = require('execa')
|
6 | 6 | const fs = require('fs')
|
@@ -37,6 +37,35 @@ function saveCoverage(coverage) {
|
37 | 37 | writeFileSync(nycFilename, JSON.stringify(coverage, null, 2))
|
38 | 38 | }
|
39 | 39 |
|
| 40 | +/** |
| 41 | + * Looks at all coverage objects in the given JSON coverage file |
| 42 | + * and if the file is relative, and exists, changes its path to |
| 43 | + * be absolute. |
| 44 | + */ |
| 45 | +function resolvePaths(nycFilename) { |
| 46 | + const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8')) |
| 47 | + let changed |
| 48 | + Object.keys(nycCoverage).forEach(key => { |
| 49 | + const coverage = nycCoverage[key] |
| 50 | + if (coverage.path && !isAbsolute(coverage.path)) { |
| 51 | + if (existsSync(coverage.path)) { |
| 52 | + debug('resolving path %s', coverage.path) |
| 53 | + coverage.path = resolve(coverage.path) |
| 54 | + changed = true |
| 55 | + } |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + if (changed) { |
| 60 | + debug('saving updated file %s', nycFilename) |
| 61 | + writeFileSync( |
| 62 | + nycFilename, |
| 63 | + JSON.stringify(nycCoverage, null, 2) + '\n', |
| 64 | + 'utf8' |
| 65 | + ) |
| 66 | + } |
| 67 | +} |
| 68 | + |
40 | 69 | const tasks = {
|
41 | 70 | /**
|
42 | 71 | * Clears accumulated code coverage information.
|
@@ -97,6 +126,8 @@ const tasks = {
|
97 | 126 | return null
|
98 | 127 | }
|
99 | 128 |
|
| 129 | + resolvePaths(nycFilename) |
| 130 | + |
100 | 131 | if (customNycReportScript) {
|
101 | 132 | debug(
|
102 | 133 | 'saving coverage report using script "%s" from package.json, command: %s',
|
|
0 commit comments