Skip to content

Commit

Permalink
feat: implement quiet switch (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Jun 17, 2019
1 parent e951347 commit 81f5b61
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/cli/commands/__tests__/lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,35 @@ describe('lint', () => {
});
});

describe('when --quiet flag is provided', () => {
test
.stdout()
.command(['lint', invalidOas3SpecPath, '--quiet'])
.it('does not log any additional feedback', ctx => {
expect(ctx.stdout).not.toContain('OpenAPI 3.x detected');
});

test
.stdout()
.command(['lint', invalidOas3SpecPath, '--quiet', '--format=json'])
.it('outputs warnings/errors in a parseable json format', ctx => {
expect(JSON.parse(ctx.stdout)).toEqual([
expect.objectContaining({
message: 'Info object should contain `contact` object.',
code: 'info-contact',
}),
expect.objectContaining({
code: 'info-description',
message: 'OpenAPI object info `description` must be present and non-empty string.',
}),
expect.objectContaining({
code: 'api-servers',
message: 'OpenAPI `servers` must be present and non-empty array.',
}),
]);
});
});

describe('when loading specification files from web', () => {
test
.nock('http://foo.local', api =>
Expand Down
21 changes: 20 additions & 1 deletion src/cli/commands/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,14 @@ linting ./openapi.yaml
char: 'v',
description: 'increase verbosity',
}),
quiet: flagHelpers.boolean({
char: 'q',
description: 'no logging - output only',
}),
};

protected quiet = false;

public static args = [{ name: 'source' }];

public async run() {
Expand All @@ -77,6 +83,8 @@ linting ./openapi.yaml

let config: ILintConfig = mergeConfig(createEmptyConfig(), flags);

this.quiet = flags.quiet;

const configFile = configFileFlag || getDefaultConfigFile(process.cwd()) || null;
if (configFile) {
try {
Expand Down Expand Up @@ -105,6 +113,16 @@ linting ./openapi.yaml
this.error('You must specify a document to lint');
}
}

public log(message?: string, ...args: any[]): void {
if (!this.quiet) {
super.log(message, ...args);
}
}

public print(message?: string, ...args: any[]): void {
super.log(message, ...args);
}
}

async function tryReadOrLog(command: Lint, reader: Function) {
Expand Down Expand Up @@ -224,7 +242,7 @@ export async function writeOutput(outputStr: string, flags: any, command: Lint)
return writeFileAsync(flags.output, outputStr);
}

command.log(outputStr);
command.print(outputStr);
}

function mergeConfig(config: IConfig, flags: any): ILintConfig {
Expand All @@ -238,6 +256,7 @@ function mergeConfig(config: IConfig, flags: any): ILintConfig {
maxResults: flags.maxResults > 0 ? flags.maxResults : flags['max-results'],
verbose: flags.verbose,
ruleset: flags.ruleset,
quiet: flags.quiet,
skipRule: flags['skip-rule'],
},
isNil,
Expand Down

0 comments on commit 81f5b61

Please sign in to comment.