all files / src/ index.js

100% Statements 12/12
66.67% Branches 4/6
100% Functions 3/3
100% Lines 11/11
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                                                                        
const fs = require('fs');
const remap = require('./remap');
const { Report, Store } = require('istanbul');
/**
 * The basic API for utilising remap-istanbul
 * @param  {Array} sources The sources that could be consumed and remapped.
 *                                For muliple sources to be combined together, provide
 *                                an array of strings.
 * @param  {Object} reports An object where each key is the report type required and the value
 *                          is report options
 * @param  {Object} remapOptions An object with options for remapping
 *               exclude?  - a string or Regular Expression that filters out
 *                           any coverage where the file path matches
 *               readFile? - a function that can read a file
 *                           syncronously
 *               readJSON? - a function that can read and parse a
 *                           JSON file syncronously
 *               sources?  - a Istanbul store where inline sources will be
 *                           added
 *               warn?     - a function that logs warnings
 */
module.exports = function (sources, reports, remapOptions = {}) {
  const sourceStore = Store.create('memory');
  const collector = remap(
    sources.map(filePath => JSON.parse(fs.readFileSync(filePath))),
    Object.assign(
      {}, remapOptions, {
        sources: sourceStore,
      }
    ));
 
  Object.keys(reports)
    .forEach(
      reportType => {
        const options = Object.assign(
          Object.keys(sourceStore.map).length ? { sourceStore } : {},
          reports[reportType]
        );
        const reporter = Report.create(reportType, options);
        reporter.writeReport(collector, true);
      }
    );
};
 
module.exports.remap = remap;