From c57bd741534813a7b42c8f300ddb61ef42086ef1 Mon Sep 17 00:00:00 2001 From: Jonathan Santerre Date: Mon, 4 Nov 2019 13:52:13 -0500 Subject: [PATCH] fix: write to disk incrementally (#40) @SanterreJo thank you for the contribution :+1: I'll make an effort to get a release out soon, if I get buried in other work, please feel free to contact me here: http://devtoolscommunity.herokuapp.com/ --- lib/puppeteer-to-istanbul.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/puppeteer-to-istanbul.js b/lib/puppeteer-to-istanbul.js index 3d350c8..f8471b0 100644 --- a/lib/puppeteer-to-istanbul.js +++ b/lib/puppeteer-to-istanbul.js @@ -16,7 +16,14 @@ class PuppeteerToIstanbul { } writeIstanbulFormat () { - var fullJson = {} + mkdirp.sync('./.nyc_output') + + const outFilePath = './.nyc_output/out.json' + + fs.writeFileSync(outFilePath, '') + + const fd = fs.openSync(outFilePath, 'a') + fs.writeSync(fd, '{') this.puppeteerToV8Info.forEach(jsFile => { const script = v8toIstanbul(jsFile.url) @@ -25,12 +32,18 @@ class PuppeteerToIstanbul { let istanbulCoverage = script.toIstanbul() let keys = Object.keys(istanbulCoverage) - fullJson[keys[0]] = istanbulCoverage[keys[0]] - fullJson[keys[0]].originalUrl = jsFile.originalUrl + let jsonPart = {} + jsonPart[keys[0]] = istanbulCoverage[keys[0]] + jsonPart[keys[0]].originalUrl = jsFile.originalUrl + + let jsonStr = JSON.stringify(jsonPart) + .replace(/^{/, '') + .replace(/}$/, '') + fs.writeSync(fd, jsonStr) }) - mkdirp.sync('./.nyc_output') - fs.writeFileSync('./.nyc_output/out.json', JSON.stringify(fullJson), 'utf8') + fs.writeSync(fd, '}') + fs.closeSync(fd) } }