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 option to print config #208

Open
wants to merge 1 commit into
base: master
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
17 changes: 17 additions & 0 deletions src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export function run(stdout, stdin, stderr, argv) {
'-e, --except <rules>',
'This option is DEPRECATED. Use `--rules` instead.'
)
.option(
'--print-config',
'print the configuration if valid and exit with 0'
)
.version(version, '--version')
.parse(argv);

Expand Down Expand Up @@ -83,6 +87,12 @@ export function run(stdout, stdin, stderr, argv) {
return 2;
}

if (commander.printConfig) {
const { schema, sourceMap, stdinFd, ...strippedConfig } = configuration;
stdout.write(JSON.stringify(strippedConfig, stringifyRule, 2) + '\n');
return 0;
}

const schema = configuration.getSchema();
if (schema == null) {
console.error('No valid schema input.');
Expand All @@ -100,6 +110,13 @@ export function run(stdout, stdin, stderr, argv) {
return errors.length > 0 ? 1 : 0;
}

function stringifyRule(key, value) {
if (typeof value === 'function') {
return value.name;
}
return value;
}

function groupErrorsBySchemaFilePath(errors, schemaSourceMap) {
return errors.reduce((groupedErrors, error) => {
const path = schemaSourceMap.getOriginalPathForLine(
Expand Down
30 changes: 30 additions & 0 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,5 +430,35 @@ describe('Runner', () => {
);
assert.equal(0, exitCode);
});

it('prints config and exits with 0 if config is valid', () => {
const argv = [
'node',
'lib/cli.js',
'--print-config',
'--rules',
'no-rule-of-mine,fields-have-descriptions',
'--stdin',
'foo.graphql',
'--format',
'json',
'--custom-rule-paths',
`${__dirname}/fixtures/custom_rules/*`,
];

const exitCode = run(mockStdout, mockStdin, mockStderr, argv);
assert.strictEqual(exitCode, 0);

const config = JSON.parse(stdout);
assert(config.options.stdin);
assert.strictEqual(config.options.format, 'json');
assert(!config.options.commentDescriptions);
assert(!config.options.oldImplementsSyntax);
assert.strictEqual(config.options.customRulePaths.length, 1);
assert.strictEqual(config.options.rules.length, 2);
assert(config.rules.length > 0);
assert(config.builtInRulePaths);
assert.strictEqual(config.rulePaths.length, 2);
});
});
});