| 1 |
'use strict' |
| 2 |
const path = require('path') |
| 3 |
const makeDir = require('make-dir') |
| 4 |
const fs = require('../fs-promises') |
| 5 |
const { cliWrapper, setupOptions } = require('./helpers.js') |
| 6 |
|
| 7 |
const NYC = require('../../index.js') |
| 8 |
|
| 9 |
exports.command = 'merge <input-directory> [output-file]' |
| 10 |
|
| 11 |
exports.describe = 'merge istanbul format coverage output in a given folder' |
| 12 |
|
| 13 |
exports.builder = function (yargs) { |
| 14 |
yargs |
| 15 |
.demandCommand(0, 0) |
| 16 |
.example('$0 merge ./out coverage.json', 'merge together reports in ./out and output as coverage.json') |
| 17 |
.positional('input-directory', { |
| 18 |
describe: 'directory containing multiple istanbul coverage files', |
| 19 |
type: 'text', |
| 20 |
default: './.nyc_output' |
| 21 |
}) |
| 22 |
.positional('output-file', { |
| 23 |
describe: 'file to output combined istanbul format coverage to', |
| 24 |
type: 'text', |
| 25 |
default: 'coverage.json' |
| 26 |
}) |
| 27 |
|
| 28 |
setupOptions(yargs, 'merge') |
| 29 |
yargs.default('exclude-after-remap', false) |
| 30 |
} |
| 31 |
|
| 32 |
exports.handler = cliWrapper(async argv => { |
| 33 |
process.env.NYC_CWD = process.cwd() |
| 34 |
const nyc = new NYC(argv) |
| 35 |
const inputStat = await fs.stat(argv.inputDirectory).catch(error => { |
| 36 |
throw new Error(`failed access input directory ${argv.inputDirectory} with error:\n\n${error.message}`) |
| 37 |
}) |
| 38 |
|
| 39 |
if (!inputStat.isDirectory()) { |
| 40 |
throw new Error(`${argv.inputDirectory} was not a directory`) |
| 41 |
} |
| 42 |
await makeDir(path.dirname(argv.outputFile)) |
| 43 |
const map = await nyc.getCoverageMapFromAllCoverageFiles(argv.inputDirectory) |
| 44 |
await fs.writeFile(argv.outputFile, JSON.stringify(map, null, 2), 'utf8') |
| 45 |
console.info(`coverage files in ${argv.inputDirectory} merged into ${argv.outputFile}`) |
| 46 |
}) |