Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option --no-summary to not print the summary at the end of the report #111

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});