| 1 |
'use strict' |
| 2 |
|
| 3 |
const path = require('path') |
| 4 |
const fs = require('fs') |
| 5 |
const istanbul = require('istanbul-lib-instrument') |
| 6 |
const makeDir = require('make-dir') |
| 7 |
const glob = require('glob') |
| 8 |
|
| 9 |
const instrumenter = istanbul.createInstrumenter({ |
| 10 |
coverageVariable: '___NYC_SELF_COVERAGE___', |
| 11 |
esModules: true |
| 12 |
}) |
| 13 |
|
| 14 |
function instrumentFile (name) { |
| 15 |
const indexPath = path.join(__dirname, name) |
| 16 |
const outputPath = path.join(__dirname, 'self-coverage', name) |
| 17 |
|
| 18 |
const source = fs.readFileSync(indexPath, 'utf8') |
| 19 |
const instrumentedSource = name === 'package.json' ? source : instrumenter.instrumentSync(source, indexPath) |
| 20 |
|
| 21 |
makeDir.sync(path.dirname(outputPath)) |
| 22 |
fs.writeFileSync(outputPath, instrumentedSource) |
| 23 |
} |
| 24 |
|
| 25 |
function instrumentGlob (pattern) { |
| 26 |
const result = glob.sync(pattern, { |
| 27 |
cwd: __dirname, |
| 28 |
nodir: true |
| 29 |
}) |
| 30 |
|
| 31 |
result.forEach(file => { |
| 32 |
instrumentFile(file) |
| 33 |
}) |
| 34 |
} |
| 35 |
|
| 36 |
function instrumentAll () { |
| 37 |
/* package.json is just being copied so the instrumented copy of lib/hash.js can find it. */ |
| 38 |
const globPatterns = ['package.json', 'index.js', 'bin/*.js', 'lib/**/*.js'] |
| 39 |
|
| 40 |
globPatterns.forEach(pattern => { |
| 41 |
instrumentGlob(pattern) |
| 42 |
}) |
| 43 |
} |
| 44 |
|
| 45 |
instrumentAll() |