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

feat(cli): added parser arg #2737

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions docs/guides/2-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Other options include:
-D, --display-only-failures only output results equal to or greater than --fail-severity [boolean] [default: false]
--ignore-unknown-format do not warn about unmatched formats [boolean] [default: false]
--fail-on-unmatched-globs fail on unmatched glob patterns [boolean] [default: false]
-p, --parser which parser should be used to read the file (defaults to Yaml)
[string] [choices: "Yaml", "Json"] [default: "Yaml"]
-v, --verbose increase verbosity [boolean]
-q, --quiet no logging - output only [boolean]
```
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/src/commands/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ const lintCommand: CommandModule = {
type: 'boolean',
default: false,
},
parser: {
alias: 'p',
description: 'which parser should be used to read the file (defaults to Yaml)',
type: 'string',
choices: ['Yaml', 'Json'],
default: 'Yaml',
},
verbose: {
alias: 'v',
description: 'increase verbosity',
Expand All @@ -175,6 +182,7 @@ const lintCommand: CommandModule = {
encoding,
ignoreUnknownFormat,
failOnUnmatchedGlobs,
parser,
...config
} = args as unknown as ILintConfig & {
documents: Array<number | string>;
Expand All @@ -191,6 +199,7 @@ const lintCommand: CommandModule = {
failOnUnmatchedGlobs,
ruleset,
stdinFilepath,
parser,
...pick<Partial<ILintConfig>, keyof ILintConfig>(config, ['verbose', 'quiet', 'resolver']),
});

Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/services/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Dictionary } from '@stoplight/types';
import type * as Parsers from '@stoplight/spectral-parsers';
import { HumanReadableDiagnosticSeverity } from '@stoplight/spectral-core';

export type FailSeverity = HumanReadableDiagnosticSeverity;
Expand Down Expand Up @@ -29,4 +30,5 @@ export interface ILintConfig {
failOnUnmatchedGlobs: boolean;
verbose?: boolean;
quiet?: boolean;
parser?: keyof Pick<typeof Parsers, 'Json' | 'Yaml'>;
}
18 changes: 16 additions & 2 deletions packages/cli/src/services/linter/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ export async function lint(documents: Array<number | string>, flags: ILintConfig
console.info(`Linting ${targetUri}`);
}

const document = await createDocument(targetUri, { encoding: flags.encoding }, flags.stdinFilepath ?? '<STDIN>');
const document = await createDocument(
targetUri,
{ encoding: flags.encoding },
flags.stdinFilepath ?? '<STDIN>',
flags.parser,
);

results.push(
...(await spectral.run(document, {
Expand All @@ -63,7 +68,16 @@ const createDocument = async (
identifier: string | number,
opts: IFileReadOptions,
source: string,
): Promise<Document<unknown, Parsers.YamlParserResult<unknown>>> => {
parser: ILintConfig['parser'] = 'Yaml',
) => {
if (parser === 'Json') {
if (typeof identifier === 'string') {
return new Document(await readParsable(identifier, opts), Parsers.Json, identifier);
}

return new Document(await readFileDescriptor(identifier, opts), Parsers.Json, source);
}

if (typeof identifier === 'string') {
return new Document(await readParsable(identifier, opts), Parsers.Yaml, identifier);
}
Expand Down
1 change: 1 addition & 0 deletions test-harness/scenarios/formats/too-few-outputs.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Options:
-D, --display-only-failures only output results equal to or greater than --fail-severity [boolean] [default: false]
--ignore-unknown-format do not warn about unmatched formats [boolean] [default: false]
--fail-on-unmatched-globs fail on unmatched glob patterns [boolean] [default: false]
-p, --parser which parser should be used to read the file (defaults to Yaml) [string] [choices: "Yaml", "Json"] [default: "Yaml"]
-v, --verbose increase verbosity [boolean]
-q, --quiet no logging - output only [boolean]

Expand Down
1 change: 1 addition & 0 deletions test-harness/scenarios/formats/too-many-outputs.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Options:
-D, --display-only-failures only output results equal to or greater than --fail-severity [boolean] [default: false]
--ignore-unknown-format do not warn about unmatched formats [boolean] [default: false]
--fail-on-unmatched-globs fail on unmatched glob patterns [boolean] [default: false]
-p, --parser which parser should be used to read the file (defaults to Yaml) [string] [choices: "Yaml", "Json"] [default: "Yaml"]
-v, --verbose increase verbosity [boolean]
-q, --quiet no logging - output only [boolean]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Options:
-D, --display-only-failures only output results equal to or greater than --fail-severity [boolean] [default: false]
--ignore-unknown-format do not warn about unmatched formats [boolean] [default: false]
--fail-on-unmatched-globs fail on unmatched glob patterns [boolean] [default: false]
-p, --parser which parser should be used to read the file (defaults to Yaml) [string] [choices: "Yaml", "Json"] [default: "Yaml"]
-v, --verbose increase verbosity [boolean]
-q, --quiet no logging - output only [boolean]

Expand Down
1 change: 1 addition & 0 deletions test-harness/scenarios/help-no-document.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Options:
-D, --display-only-failures only output results equal to or greater than --fail-severity [boolean] [default: false]
--ignore-unknown-format do not warn about unmatched formats [boolean] [default: false]
--fail-on-unmatched-globs fail on unmatched glob patterns [boolean] [default: false]
-p, --parser which parser should be used to read the file (defaults to Yaml) [string] [choices: "Yaml", "Json"] [default: "Yaml"]
-v, --verbose increase verbosity [boolean]
-q, --quiet no logging - output only [boolean]

Expand Down
Loading