Skip to content

Commit 9eb5590

Browse files
authoredDec 24, 2020
fix: follow verbosity rules for JSON and CSV (#216)
1 parent cf29469 commit 9eb5590

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed
 

‎src/cli.ts

+15
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,25 @@ async function main() {
131131
opts.linksToSkip = flags.skip.split(' ').filter(x => !!x);
132132
}
133133
const result = await checker.check(opts);
134+
const filteredResults = result.links.filter(link => {
135+
switch (link.state) {
136+
case LinkState.OK:
137+
return verbosity <= LogLevel.WARNING;
138+
case LinkState.BROKEN:
139+
if (verbosity > LogLevel.DEBUG) {
140+
link.failureDetails = undefined;
141+
}
142+
return verbosity <= LogLevel.ERROR;
143+
case LinkState.SKIPPED:
144+
return verbosity <= LogLevel.INFO;
145+
}
146+
});
134147
if (format === Format.JSON) {
148+
result.links = filteredResults;
135149
console.log(JSON.stringify(result, null, 2));
136150
return;
137151
} else if (format === Format.CSV) {
152+
result.links = filteredResults;
138153
const csv = await toCSV(result.links);
139154
console.log(csv);
140155
return;

‎src/logger.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class Logger {
4242
}
4343

4444
error(message?: string) {
45-
if (this.level <= LogLevel.ERROR) {
45+
if (this.level <= LogLevel.ERROR && this.format === Format.TEXT) {
4646
console.error(message);
4747
}
4848
}

‎test/zcli.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {describe, it} from 'mocha';
22
import * as execa from 'execa';
33
import {assert} from 'chai';
4+
import {LinkResult, LinkState} from '../src/index';
45

56
describe('cli', () => {
67
before(async () => {
@@ -53,7 +54,6 @@ describe('cli', () => {
5354
it('should provide CSV if asked nicely', async () => {
5455
const res = await execa('npx', [
5556
'linkinator',
56-
'--markdown',
5757
'--format',
5858
'csv',
5959
'test/fixtures/markdown/README.md',
@@ -64,12 +64,12 @@ describe('cli', () => {
6464
it('should provide JSON if asked nicely', async () => {
6565
const res = await execa('npx', [
6666
'linkinator',
67-
'--markdown',
6867
'--format',
6968
'json',
7069
'test/fixtures/markdown/README.md',
7170
]);
72-
assert.match(res.stdout, /{/);
71+
const output = JSON.parse(res.stdout);
72+
assert.ok(output.links);
7373
});
7474

7575
it('should not show links if --silent', async () => {
@@ -81,6 +81,21 @@ describe('cli', () => {
8181
assert.notMatch(res.stdout, /\[/);
8282
});
8383

84+
it('should not show 200 links if verbosity is ERROR with JSON', async () => {
85+
const res = await execa('npx', [
86+
'linkinator',
87+
'--verbosity',
88+
'ERROR',
89+
'--format',
90+
'JSON',
91+
'test/fixtures/markdown/README.md',
92+
]);
93+
const links = JSON.parse(res.stdout).links as LinkResult[];
94+
for (const link of links) {
95+
assert.strictEqual(link.state, LinkState.BROKEN);
96+
}
97+
});
98+
8499
it('should accept a server-root', async () => {
85100
const res = await execa('npx', [
86101
'linkinator',

0 commit comments

Comments
 (0)
Please sign in to comment.