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 --print-config CLI flag (fixes #3127) #3532

Merged
merged 8 commits into from Aug 15, 2018

Conversation

thibaudcolas
Copy link
Member

@thibaudcolas thibaudcolas commented Aug 12, 2018

Resolves #3127.

A nice feature of eslint's CLI is the option --print-config that will log to the console the parsed configuration object for a given configuration file, including all the extended rules. It's awesome for debugging configuration files.

This PR is largely inspired by the ESLint implementation. Also recycles its (succinct) docs. Most of the functionality is taken care of by getConfigForFile, so what I added is mostly an integration of that into the CLI, replicating how getConfigForFile gets called in lintSource.

Here are useful commands to use while testing this that should cover the various scenarios:

# The canonical example: point to a file, there's a config with extends nearby, it resolves the full config.
./bin/stylelint.js --print-config lib/__tests__/fixtures/getConfigForFile/a/b/foo.css
# Needs to be given a path.
./bin/stylelint.js --print-config
# Globs are not supported.
./bin/stylelint.js --print-config 'lib/__tests__/fixtures/getConfigForFile/**/foo.css'
# Multiple paths isn't supported either.
./bin/stylelint.js --print-config lib/__tests__/fixtures/getConfigForFile/a/b/foo.css lib/__tests__/fixtures/empty-block.css

Sample output:

$ ./bin/stylelint.js --print-config lib/__tests__/fixtures/getConfigForFile/**/foo.css
{
  "rules": {
    "block-no-empty": [
      true
    ],
    "plugin/warn-about-foo": [
      "always"
    ]
  },
  "plugins": [
    "[...]/stylelint/lib/__tests__/fixtures/plugin-warn-about-foo.js"
  ],
  "pluginFunctions": {}
}

And the errors:

$ ./bin/stylelint.js --print-config 'lib/__tests__/fixtures/getConfigForFile/**/foo.css'
Error: The --print-config option does not support globs.
    at module.exports ([...]/stylelint/lib/printConfig.js:33:7)
    at Promise.resolve.then.then.options ([...]/stylelint/lib/cli.js:478:16)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
    at Function.Module.runMain (module.js:667:11)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:607:3

$ ./bin/stylelint.js --print-config lib/__tests__/fixtures/getConfigForFile/a/b/foo.css lib/__tests__/fixtures/empty-block.css
Error: The --print-config option must be used with exactly one file path.
    at module.exports ([...]/stylelint/lib/printConfig.js:23:7)
    at Promise.resolve.then.then.options ([...]/stylelint/lib/cli.js:478:16)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
    at Function.Module.runMain (module.js:667:11)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:607:3

lib/cli.js Outdated
process.stdout.write(JSON.stringify(config, null, " "));
})
.catch((err /*: { stack: any, code: any }*/) => {
console.log(err.stack); // eslint-disable-line no-console
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need to create a new function to catch errors, not just copy these lines.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌 thanks for the suggestion, should be taken care of now. I'm not familiar with the stylelint codebase at all so let me know if there are other parts of the code that could use a refactor or two.

@jeddy3 jeddy3 mentioned this pull request Aug 13, 2018
6 tasks
Copy link
Member

@jeddy3 jeddy3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@jeddy3 jeddy3 merged commit 2dd1834 into stylelint:master Aug 15, 2018
@jeddy3
Copy link
Member

jeddy3 commented Aug 15, 2018

  • Added: --print-config CLI flag (#3532).

@thibaudcolas thibaudcolas deleted the feature/flag-print-config-3127 branch August 16, 2018 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Add --print-config flag
3 participants