From 03c86979880d6c7abd548acfea1d11a956d0fa5a Mon Sep 17 00:00:00 2001 From: calixteman Date: Mon, 18 Nov 2024 00:48:31 +0100 Subject: [PATCH] feat: add an option --no-summary to not print the summary at the end of the report (#111) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add an option --no-summary to not print the summary at the end of the report * Format, fix error and add tests --------- Co-authored-by: Álvaro Mondéjar Rubio --- bin/cli.js | 10 ++++++---- src/cli/gui.js | 21 +++++++++++++++------ test/cli.spec.js | 18 ++++++++++++++++++ 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index 84de543..d7145b4 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -14,8 +14,6 @@ import logging from '../src/lib/logger.js'; import SVGLint from '../src/svglint.js'; // @ts-ignore -const gui = new GUI(); - const logger = logging(''); const EXIT_CODES = Object.freeze({ @@ -46,7 +44,7 @@ const cli = meow( ` ${chalk.yellow('Usage:')} ${chalk.bold('svglint')} [--config config.js] [--ci] [--debug] ${chalk.bold('file1.svg file2.svg')} - ${chalk.bold('svglint')} --stdin [--config config.js] [--ci] [--debug] < ${chalk.bold('file1.svg')} + ${chalk.bold('svglint')} --stdin [--config config.js] [--summary] [--ci] [--debug] < ${chalk.bold('file1.svg')} ${chalk.yellow('Options:')} ${chalk.bold('--help')} Display this help text @@ -54,7 +52,8 @@ const cli = meow( ${chalk.bold('--config, -c')} Specify the config file. Defaults to '.svglintrc.js' ${chalk.bold('--debug, -d')} Show debug logs ${chalk.bold('--ci, -C')} Only output to stdout once, when linting is finished - ${chalk.bold('--stdin')} Read an SVG from stdin`, + ${chalk.bold('--stdin')} Read an SVG from stdin + ${chalk.bold('--summary')} Print the summary at the end`, { importMeta: import.meta, flags: { @@ -62,10 +61,13 @@ const cli = meow( debug: {type: 'boolean', alias: 'd'}, ci: {type: 'boolean', alias: 'C'}, stdin: {type: 'boolean'}, + summary: {type: 'boolean', default: true}, }, }, ); +const gui = new GUI({printSummary: cli.flags.summary}); + process.on('exit', () => { gui.finish(); }); diff --git a/src/cli/gui.js b/src/cli/gui.js index 9219f0e..2c21d63 100644 --- a/src/cli/gui.js +++ b/src/cli/gui.js @@ -15,7 +15,7 @@ const logHistory = Logger.cliConsole; /** @typedef {import("../lib/linting.js")} Linting */ export default class GUI { - constructor() { + constructor({printSummary = true} = {}) { // Subscribe to global logs Logger.setCLI(true); logHistory.on('msg', () => this.update()); @@ -30,7 +30,7 @@ export default class GUI { summary: new Separator('Summary'), }; this.$log = new Log(logHistory); - this.$summary = new Summary(); + this.$summary = printSummary ? new Summary() : null; /** @type {LintingDisplay[]} */ this.$lintings = []; } @@ -40,8 +40,11 @@ export default class GUI { */ finish() { if (this.ci) { - // eslint-disable-next-line no-console - console.log(this.render()); + const output = this.render(); + if (output) { + // eslint-disable-next-line no-console + console.log(output); + } } else { this.update(true); } @@ -104,7 +107,10 @@ export default class GUI { } } - outp.push('', this.$titles.summary, this.$summary); + if (this.$summary) { + outp.push('', this.$titles.summary, this.$summary); + } + if (outp[0] === '') { outp.shift(); } @@ -127,7 +133,10 @@ export default class GUI { */ addLinting(linting) { this.$lintings.push(new LintingDisplay(linting)); - this.$summary.addLinting(linting); + if (this.$summary) { + this.$summary.addLinting(linting); + } + linting.on('rule', () => this.update()); linting.on('done', () => this.update()); } diff --git a/test/cli.spec.js b/test/cli.spec.js index 564097c..bf768c4 100644 --- a/test/cli.spec.js +++ b/test/cli.spec.js @@ -144,3 +144,21 @@ describe('Configuration files', function () { expect(stdout).not.toMatch('Failed to lint'); }); }); + +describe('--no-summary', function () { + it('should not print summary with valid SVG', async function () { + const {stdout} = await execCli([VALID_SVG, '--no-summary']); + expect(stdout).not.toMatch('Summary'); + }); + + it('should print the summary not passing --no-summary', async function () { + const {stdout} = await execCli([VALID_SVG]); + expect(stdout).toMatch('Summary'); + }); + + it('should print errors with invalid SVG', async function () { + const {stdout} = await execCli([INVALID_SVG, '--no-summary']); + expect(stdout).toMatch('Files'); + expect(stdout).not.toMatch('Summary'); + }); +});