Skip to content

Commit

Permalink
feat: add an option --no-summary to not print the summary at the end …
Browse files Browse the repository at this point in the history
…of the report (#111)

* 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 <[email protected]>
  • Loading branch information
calixteman and mondeja authored Nov 17, 2024
1 parent 0a78c5b commit 03c8697
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
10 changes: 6 additions & 4 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -46,26 +44,30 @@ 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
${chalk.bold('--version')} Show the current SVGLint version
${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: {
config: {type: 'string', alias: 'c'},
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();
});
Expand Down
21 changes: 15 additions & 6 deletions src/cli/gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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 = [];
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -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());
}
Expand Down
18 changes: 18 additions & 0 deletions test/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

0 comments on commit 03c8697

Please sign in to comment.