Skip to content

Commit d18d49b

Browse files
committed
chore: refactor
1 parent 20a2a9a commit d18d49b

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

task.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ function saveCoverage(coverage) {
3838
}
3939

4040
/**
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.
41+
* A small debug utility to inspect paths saved in NYC output JSON file
4442
*/
45-
function resolvePaths(nycFilename) {
43+
function showNycInfo(nycFilename) {
4644
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))
4745

4846
const coverageKeys = Object.keys(nycCoverage)
@@ -52,17 +50,39 @@ function resolvePaths(nycFilename) {
5250
}
5351
debug('NYC file %s has %d key(s)', nycFilename, coverageKeys.length)
5452

55-
let changed
5653
const maxPrintKeys = 3
54+
const showKeys = coverageKeys.slice(0, maxPrintKeys)
5755

58-
Object.keys(nycCoverage).forEach((key, k) => {
56+
showKeys.forEach((key, k) => {
5957
const coverage = nycCoverage[key]
6058

6159
// printing a few found keys and file paths from the coverage file
6260
// will make debugging any problems much much easier
6361
if (k < maxPrintKeys) {
6462
debug('%d key %s file path %s', k + 1, key, coverage.path)
6563
}
64+
})
65+
}
66+
67+
/**
68+
* Looks at all coverage objects in the given JSON coverage file
69+
* and if the file is relative, and exists, changes its path to
70+
* be absolute.
71+
*/
72+
function resolvePaths(nycFilename) {
73+
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))
74+
75+
const coverageKeys = Object.keys(nycCoverage)
76+
if (!coverageKeys.length) {
77+
console.error('⚠️ file %s has no coverage information', nycFilename)
78+
return
79+
}
80+
debug('NYC file %s has %d key(s)', nycFilename, coverageKeys.length)
81+
82+
let changed
83+
84+
coverageKeys.forEach((key, k) => {
85+
const coverage = nycCoverage[key]
6686

6787
if (coverage.path && !isAbsolute(coverage.path)) {
6888
if (existsSync(coverage.path)) {
@@ -143,6 +163,7 @@ const tasks = {
143163
return null
144164
}
145165

166+
showNycInfo(nycFilename)
146167
resolvePaths(nycFilename)
147168

148169
if (customNycReportScript) {

0 commit comments

Comments
 (0)