Skip to content

Commit

Permalink
feat: load coverage files individually instead of all at once, addres…
Browse files Browse the repository at this point in the history
…sing memory issues (#806)
  • Loading branch information
RyanV authored and bcoe committed Apr 7, 2018
1 parent dd372f5 commit 05fea60
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
28 changes: 22 additions & 6 deletions index.js
Expand Up @@ -425,7 +425,7 @@ NYC.prototype._getCoverageMapFromAllCoverageFiles = function () {
var _this = this
var map = libCoverage.createCoverageMap({})

this.loadReports().forEach(function (report) {
this.eachReport(function (report) {
map.merge(report)
})
// depending on whether source-code is pre-instrumented
Expand Down Expand Up @@ -514,26 +514,42 @@ NYC.prototype._loadProcessInfos = function () {
})
}

NYC.prototype.loadReports = function (filenames) {
NYC.prototype.eachReport = function (filenames, iterator) {
if (typeof filenames === 'function') {
iterator = filenames
filenames = undefined
}

var _this = this
var files = filenames || fs.readdirSync(this.tempDirectory())

return files.map(function (f) {
files.forEach(function (f) {
var report
try {
report = JSON.parse(fs.readFileSync(
path.resolve(_this.tempDirectory(), f),
'utf-8'
))

_this.sourceMaps.reloadCachedSourceMaps(report)
} catch (e) { // handle corrupt JSON output.
return {}
report = {}
}

_this.sourceMaps.reloadCachedSourceMaps(report)
return report
iterator(report)
})
}

NYC.prototype.loadReports = function (filenames) {
var reports = []

this.eachReport(filenames, function (report) {
reports.push(report)
})

return reports
}

NYC.prototype.tempDirectory = function () {
return path.resolve(this.cwd, this._tempDirectory)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/process.js
Expand Up @@ -81,7 +81,7 @@ ProcessInfo.prototype.render = function (nyc) {
this.getCoverageMap(function (filenames, maps) {
var map = libCoverage.createCoverageMap({})

nyc.loadReports(filenames).forEach(function (report) {
nyc.eachReport(filenames, function (report) {
map.merge(report)
})

Expand Down

0 comments on commit 05fea60

Please sign in to comment.