Skip to content

Commit cf82cfa

Browse files
committed
feat: allow a custom storagePath
1 parent b3c12ca commit cf82cfa

5 files changed

Lines changed: 35 additions & 18 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Convert coverage from the format outputted by [puppeteer](https://developers.goo
3333
page.coverage.stopJSCoverage(),
3434
page.coverage.stopCSSCoverage(),
3535
]);
36-
pti.write([...jsCoverage, ...cssCoverage])
36+
pti.write([...jsCoverage, ...cssCoverage], { storagePath: './.nyc_output' })
3737
await browser.close()
3838
})()
3939
```

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const PuppeteerToIstanbul = require('./lib/puppeteer-to-istanbul')
22

33
module.exports = {
4-
write: (puppeteerFormat) => {
5-
const pti = PuppeteerToIstanbul(puppeteerFormat)
6-
pti.writeIstanbulFormat()
4+
write: (puppeteerFormat, options) => {
5+
const pti = PuppeteerToIstanbul(puppeteerFormat, options)
6+
pti.writeIstanbulFormat(options)
77
}
88
}

lib/output-files.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ const clone = require('clone')
99
const pathLib = require('path')
1010
const url = require('url')
1111

12-
const storagePath = './.nyc_output/js'
1312
let iterator = {}
1413

1514
class OutputFiles {
16-
constructor (coverageInfo) {
15+
constructor (coverageInfo, options) {
16+
options = options || {}
17+
options.storagePath = options.storagePath || './.nyc_output'
18+
this.storagePath = `${options.storagePath}/js`
19+
1720
// Clone coverageInfo to prevent mutating the passed in data
1821
this.coverageInfo = clone(coverageInfo)
1922
this._parseAndIsolate()
@@ -53,11 +56,11 @@ class OutputFiles {
5356
// Special case: when html present, strip and return specialized string
5457
if (pathLib.extname(parsedPath) === '.html') {
5558
isInline = true
56-
parsedPath = pathLib.resolve(storagePath, parsedPath + 'puppeteerTemp-inline')
59+
parsedPath = pathLib.resolve(this.storagePath, parsedPath + 'puppeteerTemp-inline')
5760
} else {
58-
parsedPath = pathLib.resolve(storagePath, pathLib.dirname(parsedPath), pathLib.basename(parsedPath, '.js'))
61+
parsedPath = pathLib.resolve(this.storagePath, pathLib.dirname(parsedPath), pathLib.basename(parsedPath, '.js'))
5962
}
60-
mkdirp.sync(storagePath)
63+
mkdirp.sync(this.storagePath)
6164
if (fs.existsSync(parsedPath + '.js') && isInline) {
6265
if (!Number.isInteger(iterator[parsedPath])) {
6366
iterator[parsedPath] = 1
@@ -90,8 +93,8 @@ class OutputFiles {
9093
}
9194
}
9295

93-
function genOutputFiles (coverageInfo) {
94-
return new OutputFiles(coverageInfo)
96+
function genOutputFiles (coverageInfo, options) {
97+
return new OutputFiles(coverageInfo, options)
9598
}
9699

97100
genOutputFiles.resetIterator = function () {

lib/puppeteer-to-istanbul.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ const v8toIstanbul = require('v8-to-istanbul')
77
let jsonPart = {}
88

99
class PuppeteerToIstanbul {
10-
constructor (coverageInfo) {
10+
constructor (coverageInfo, options) {
11+
options = options || {}
12+
options.storagePath = options.storagePath || './.nyc_output'
13+
1114
this.coverageInfo = coverageInfo
12-
this.puppeteerToConverter = OutputFiles(coverageInfo).getTransformedCoverage()
15+
this.options = options
16+
this.puppeteerToConverter = OutputFiles(coverageInfo, options).getTransformedCoverage()
1317
this.puppeteerToV8Info = PuppeteerToV8(this.puppeteerToConverter).convertCoverage()
1418
}
1519

@@ -18,9 +22,9 @@ class PuppeteerToIstanbul {
1822
}
1923

2024
writeIstanbulFormat () {
21-
mkdirp.sync('./.nyc_output')
25+
mkdirp.sync(this.options.storagePath)
2226

23-
const outFilePath = './.nyc_output/out.json'
27+
const outFilePath = `${this.options.storagePath}/out.json`
2428

2529
fs.writeFileSync(outFilePath, '')
2630

@@ -61,8 +65,8 @@ function mergeCoverageData (obja, objb) {
6165
return obja
6266
}
6367

64-
function genPuppeteerToIstanbul (coverageInfo) {
65-
return new PuppeteerToIstanbul(coverageInfo)
68+
function genPuppeteerToIstanbul (coverageInfo, options) {
69+
return new PuppeteerToIstanbul(coverageInfo, options)
6670
}
6771

6872
genPuppeteerToIstanbul.resetJSONPart = function () {

test/puppeteer-to-istanbul.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const OutputFiles = require('../lib/output-files')
77
var PuppeteerToIstanbul = require('../lib/puppeteer-to-istanbul')
88

99
describe('puppeteer-to-istanbul', () => {
10-
it('outputs a valid out.json file', () => {
10+
it('outputs a valid out.json file, to the default location', () => {
1111
const fixture = require('./fixtures/two-inline.json')
1212
const pti = PuppeteerToIstanbul(fixture)
1313
pti.writeIstanbulFormat()
@@ -17,6 +17,16 @@ describe('puppeteer-to-istanbul', () => {
1717
fs.unlinkSync('.nyc_output/out.json')
1818
})
1919

20+
it('outputs a valid out.json file, in the custom location', () => {
21+
const fixture = require('./fixtures/two-inline.json')
22+
const pti = PuppeteerToIstanbul(fixture, { storagePath: '.nyc_output/custom' })
23+
pti.writeIstanbulFormat()
24+
const content = fs.readFileSync('.nyc_output/custom/out.json', 'utf8')
25+
const jsonObject = JSON.parse(content)
26+
should.exist(jsonObject)
27+
fs.unlinkSync('.nyc_output/custom/out.json')
28+
})
29+
2030
it('correctly sets coverage info', () => {
2131
const fixture = require('./fixtures/two-inline.json')
2232
const pti = PuppeteerToIstanbul(fixture)

0 commit comments

Comments
 (0)