-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
build-self-coverage.js
45 lines (35 loc) · 1.15 KB
/
build-self-coverage.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
'use strict'
const path = require('path')
const fs = require('fs')
const istanbul = require('istanbul-lib-instrument')
const makeDir = require('make-dir')
const glob = require('glob')
const instrumenter = istanbul.createInstrumenter({
coverageVariable: '___NYC_SELF_COVERAGE___',
esModules: true
})
function instrumentFile (name) {
const indexPath = path.join(__dirname, name)
const outputPath = path.join(__dirname, 'self-coverage', name)
const source = fs.readFileSync(indexPath, 'utf8')
const instrumentedSource = name === 'package.json' ? source : instrumenter.instrumentSync(source, indexPath)
makeDir.sync(path.dirname(outputPath))
fs.writeFileSync(outputPath, instrumentedSource)
}
function instrumentGlob (pattern) {
const result = glob.sync(pattern, {
cwd: __dirname,
nodir: true
})
result.forEach(file => {
instrumentFile(file)
})
}
function instrumentAll () {
/* package.json is just being copied so the instrumented copy of lib/hash.js can find it. */
const globPatterns = ['package.json', 'index.js', 'bin/*.js', 'lib/**/*.js']
globPatterns.forEach(pattern => {
instrumentGlob(pattern)
})
}
instrumentAll()