-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
output-files.js
104 lines (83 loc) · 2.82 KB
/
output-files.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// output JavaScript bundled in puppeteer output to format
// that can be eaten by Istanbul.
// TODO: Put function interfaces on this file
const fs = require('fs')
const mkdirp = require('mkdirp')
const clone = require('clone')
const pathLib = require('path')
const url = require('url')
let iterator = {}
class OutputFiles {
constructor (coverageInfo, options = {}) {
this.storagePath = options.storagePath || './.nyc_output/js'
this.filePathTransformer = options.transformPath || ((filePath) => filePath)
this.includeHostname = options.hasOwnProperty('includeHostname') ? options.includeHostname : true
// Clone coverageInfo to prevent mutating the passed in data
this.coverageInfo = clone(coverageInfo)
this._parseAndIsolate()
}
parsePath (path) {
let urlPath
try {
urlPath = new url.URL(path)
} catch (error) {
path = 'file://' + path
urlPath = new url.URL(path)
}
let postProtocolPath = urlPath.pathname.substring(1)
if (urlPath.hostname && this.includeHostname) {
let hostnameAndPort = urlPath.hostname
if (urlPath.port) {
hostnameAndPort = hostnameAndPort + '_' + urlPath.port
}
postProtocolPath = hostnameAndPort + '/' + postProtocolPath
}
return postProtocolPath
}
rewritePath (path) {
// generate a new path relative to ./coverage/js.
// this would be around where you'd use mkdirp.
let str = ``
let parsedPath = this.parsePath(path)
let isInline = false
// Special case: when html present, strip and return specialized string
if (pathLib.extname(parsedPath) === '.html') {
isInline = true
parsedPath = pathLib.resolve(this.storagePath, parsedPath + 'puppeteerTemp-inline')
} else {
parsedPath = pathLib.resolve(this.storagePath, pathLib.dirname(parsedPath), pathLib.basename(parsedPath, '.js'))
}
mkdirp.sync(this.storagePath)
if (fs.existsSync(parsedPath + '.js') && isInline) {
if (!Number.isInteger(iterator[parsedPath])) {
iterator[parsedPath] = 1
} else {
iterator[parsedPath]++
}
str = `${parsedPath}-${iterator[parsedPath]}.js`
return str
} else {
str = `${parsedPath}.js`
return str
}
}
_parseAndIsolate () {
for (let i = 0; i < this.coverageInfo.length; i++) {
let path = this.rewritePath(this.coverageInfo[i].url)
this.coverageInfo[i].originalUrl = this.coverageInfo[i].url
this.coverageInfo[i].url = path
mkdirp.sync(pathLib.parse(path).dir)
fs.writeFileSync(path, this.coverageInfo[i].text)
}
}
getTransformedCoverage () {
return this.coverageInfo
}
}
function genOutputFiles (coverageInfo, options) {
return new OutputFiles(coverageInfo, options)
}
genOutputFiles.resetIterator = function () {
iterator = {}
}
module.exports = genOutputFiles