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

3 participants
@thibaudcolas
Member

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

This comment has been minimized.

@gucong3000

gucong3000 Aug 13, 2018

Member

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

This comment has been minimized.

@thibaudcolas

thibaudcolas Aug 13, 2018

Member

👌 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 referenced this pull request Aug 13, 2018

Closed

Release 9.5.0 #3511

6 of 6 tasks complete
@jeddy3

jeddy3 approved these changes Aug 15, 2018

LGTM, thanks!

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

3 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+0.06%) to 96.157%
Details
@jeddy3

This comment has been minimized.

Member

jeddy3 commented Aug 15, 2018

  • Added: --print-config CLI flag (#3532).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment