diff --git a/lib/default/options.js b/lib/default/options.js index a7169e2a..85a9adba 100644 --- a/lib/default/options.js +++ b/lib/default/options.js @@ -1,23 +1,16 @@ -const defaultOptions = { +module.exports = { - // logging levels: off, error, info, debug + // (String) logging levels: off, error, info, debug logging: 'info', - // (String) + // (String) output dir outputDir: './coverage-reports', // (String) v8 or html for istanbul by default // (Array) multiple reports with options + // v8 report or istanbul supported reports // reports: [ - // ['v8', { - // // report name (V8 only) - // name: 'Coverage Report', - // outputFile: "index.html", - // // (Boolean) Whether inline all scripts to the single HTML file. (V8 only) - // inline: false, - // // assets path if not inline. (V8 only) - // assetsPath: './assets' - // }], + // ['v8'], // ['html', { // subdir: 'my-sub-dir' // }], @@ -25,7 +18,19 @@ const defaultOptions = { // ], reports: '', - // (Function) A filter function to execute for each element in the V8 list. (V8 only) + // (String) Report name. Defaults to "Coverage Report". + name: 'Coverage Report', + + // [V8 only](String) Output [sub dir/]filename. Defaults to "index.html" + outputFile: 'index.html', + + // [V8 only](Boolean) Whether inline all scripts to the single HTML file. Defaults to false. + inline: false, + + // [V8 only](String) Assets path if not inline. Defaults to "./assets" + assetsPath: './assets', + + // [V8 only](Function) A filter function to execute for each element in the V8 list. // entryFilter: (entry) => { // if (entry.url.indexOf('googleapis.com') !== -1) { // return false; @@ -34,10 +39,12 @@ const defaultOptions = { // }, entryFilter: null, - // (Function) A filter function to execute for each element in the sources which unpacked from the source map. (V8 only) + // [V8 only](Function) A filter function to execute for each element in the sources which unpacked from the source map. // sourceFilter: (sourcePath) => sourcePath.search(/src\/.+/) !== -1, sourceFilter: null, + // [Istanbul only] defaultSummarizer, sourceFinder + // (Function) source path handler. // sourcePath: (filePath) => `wwwroot/${filePath}`, sourcePath: null, @@ -46,16 +53,3 @@ const defaultOptions = { // (Object) Istanbul: { statements:[50,80], functions:[50,80], branches:[50,80], lines:[50,80] }, V8: { bytes:[50,80] }. watermarks: [50, 80] }; - -const defaultV8ReportOptions = { - name: 'Coverage Report', - outputFile: 'index.html', - inline: false, - assetsPath: './assets' -}; - - -module.exports = { - defaultOptions, - defaultV8ReportOptions -}; diff --git a/lib/generate.js b/lib/generate.js index 6644f0b2..5e87af69 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -10,8 +10,6 @@ const { mergeV8Coverage, saveV8Report } = require('./v8/v8.js'); const { convertV8List } = require('./converter/converter.js'); -const { defaultV8ReportOptions } = require('./default/options.js'); - // ======================================================================================================== // maybe v8 or to istanbul reports @@ -28,11 +26,7 @@ const generateV8ListReports = async (v8list, coverageData, fileSources, options) // generate v8 report if (v8) { - const v8ReportOptions = { - ... defaultV8ReportOptions, - ... v8 - }; - report = await saveV8Report(v8list, options, v8ReportOptions); + report = await saveV8Report(v8list, options); } return report; diff --git a/lib/index.d.ts b/lib/index.d.ts index 4acf8a55..3d38bb67 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -11,42 +11,45 @@ export type V8CoverageEntry = { functions?: any[] } -export type V8ReportOptions = { - // report name. Defaults to "Coverage Report". - name?: string, - - // output dir and filename. Defaults to "index.html" - outputFile?: string, - - // Whether inline all scripts to the single HTML file. Defaults to false. - inline?: boolean, - - // assets path if not inline. Defaults to "./assets" - assetsPath?: string -} - export type ReportDescription = - ['v8'] | ['v8', V8ReportOptions] | + ['v8'] | // html, html-spa, json, lcov and so on // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/istanbul-reports/index.d.ts [string] | [string, any]; export type CoverageReportOptions = { - // logging levels: off, error, info, debug + // (String) logging levels: off, error, info, debug logging?: string, + // (String) output dir outputDir?: string, + // (String) v8 or html for istanbul by default + // (Array) multiple reports with options // v8 report or istanbul supported reports reports?: string | ReportDescription[], - // (Function) A filter function to execute for each element in the V8 list. (V8 only) + // (String) Report name. Defaults to "Coverage Report". + name?: string, + + // [V8 only](String) Output [sub dir/]filename. Defaults to "index.html" + outputFile?: string, + + // [V8 only](Boolean) Whether inline all scripts to the single HTML file. Defaults to false. + inline?: boolean, + + // [V8 only](String) Assets path if not inline. Defaults to "./assets" + assetsPath?: string + + // [V8 only](Function) A filter function to execute for each element in the V8 list. entryFilter?: (entry: V8CoverageEntry) => boolean, - // (Function) A filter function to execute for each element in the sources which unpacked from the source map. (V8 only) + // [V8 only](Function) A filter function to execute for each element in the sources which unpacked from the source map. sourceFilter?: (sourcePath: string) => boolean, + // [Istanbul only] defaultSummarizer, sourceFinder + // (Function) source path handler. sourcePath?: (filePath: string) => string, diff --git a/lib/index.js b/lib/index.js index ead28876..2a7cf1e3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,6 @@ const fs = require('fs'); const path = require('path'); -const { defaultOptions } = require('./default/options.js'); +const defaultOptions = require('./default/options.js'); const Util = require('./utils/util.js'); const { initV8ListAndSourcemap } = require('./v8/v8.js'); diff --git a/lib/v8/v8.js b/lib/v8/v8.js index 9ff8e979..3599acfb 100644 --- a/lib/v8/v8.js +++ b/lib/v8/v8.js @@ -167,11 +167,9 @@ const mergeV8Coverage = async (dataList, options) => { // ============================================================ -const saveV8HtmlReport = async (reportData, v8ReportOptions) => { +const saveV8HtmlReport = async (reportData, options) => { - const { - inline, assetsPath, outputFile - } = v8ReportOptions; + const outputFile = path.resolve(options.outputDir, options.outputFile); const outputDir = path.dirname(outputFile); const htmlFile = path.basename(outputFile); @@ -186,24 +184,24 @@ const saveV8HtmlReport = async (reportData, v8ReportOptions) => { // console.log(jsFiles); - const options = { - inline, + const htmlOptions = { reportData, jsFiles, - assetsPath, + inline: options.inline, + assetsPath: options.assetsPath, outputDir, htmlFile, reportDataFile: 'coverage-data.js' }; - const htmlPath = await Util.saveHtmlReport(options); + const htmlPath = await Util.saveHtmlReport(htmlOptions); return htmlPath; }; -const saveV8Report = async (v8list, options, v8ReportOptions) => { +const saveV8Report = async (v8list, options) => { const defaultWatermarks = { bytes: [50, 80] }; @@ -244,22 +242,18 @@ const saveV8Report = async (v8list, options, v8ReportOptions) => { }; const htmlOptions = { - name: v8ReportOptions.name, + name: options.name, watermarks, summary, files: v8list }; - // init html dir and filename - const outputFile = path.resolve(options.outputDir, v8ReportOptions.outputFile); - v8ReportOptions.outputFile = outputFile; - - const htmlPath = await saveV8HtmlReport(htmlOptions, v8ReportOptions); + const htmlPath = await saveV8HtmlReport(htmlOptions, options); const report = { type: 'v8', htmlPath, - name: v8ReportOptions.name, + name: options.name, watermarks, summary, files: summaryList