diff --git a/src/cli/cli.ts b/src/cli/cli.ts index 8b9bde8..33bbbdd 100644 --- a/src/cli/cli.ts +++ b/src/cli/cli.ts @@ -5,6 +5,7 @@ import { program } from './program.js' import { read } from './file-reader.js' import { print as pretty } from './reporters/pretty.js' import { print as tap } from './reporters/tap.js' +import { print as json } from './reporters/json.js' import { help } from './help.js' async function cli(cli_args: string[]) { @@ -30,8 +31,7 @@ async function cli(cli_args: string[]) { return tap(report, params) } if (params.reporter === 'json') { - let log = JSON.stringify(report) - return report.report.ok ? console.log(log) : console.error(log) + return json(report, params) } return pretty(report, params) } diff --git a/src/cli/reporters/json.ts b/src/cli/reporters/json.ts new file mode 100644 index 0000000..23d4569 --- /dev/null +++ b/src/cli/reporters/json.ts @@ -0,0 +1,34 @@ +import type { CliArguments } from '../arguments.js' +import type { Report } from '../program.js' + +function prepare({ report, context }: Report, params: CliArguments) { + context.coverage.coverage_per_stylesheet = context.coverage.coverage_per_stylesheet.filter((sheet) => { + // Include if the user wanted min-file-coverage and this file coverage is too low + if ( + params['show-uncovered'] === 'violations' && + report.min_file_line_coverage.expected !== undefined && + sheet.line_coverage_ratio < report.min_file_line_coverage.expected + ) { + return true + } + + // Include if show=all and coverage isn't 100% + if (params['show-uncovered'] === 'all' && sheet.line_coverage_ratio < 1) { + return true + } + + // Skip the sheet if show=none or coverage is higher than requested + return false + }) + + return { + report, + context, + } +} + +export function print({ report, context }: Report, params: CliArguments): void { + let logger = report.ok ? console.log : console.error + let data = prepare({ context, report }, params) + logger(JSON.stringify(data)) +}