跳到主要内容

命令行工具 (CLI)

你可以通过命令行(或终端)来运行 Stylelint。示例如下:

npx stylelint "**/*.css"

通过 npx stylelint --help 命令来输出命令行工具(CLI)的文档。

参数

命令行工具(CLI)可以接受以下参数:

--allow-empty-input, --aei

当 glob 模式匹配不到任何文件时,进程将退出且不抛出任何错误。 了解更多信息

--cache-location

为缓存指定一个文件或目录。详细信息见这里

--cache

存储经过处理过的文件,以便 Stylelint 仅对已更改的文件进行操作。默认情况下,缓存的位置是 process.cwd() 目录下的 ./.stylelintcache 目录。详细信息见这里

--color, --no-color

强制启用/禁用彩色输出。Force enabling/disabling of color.

--config-basedir

Absolute path to the directory that relative paths defining "extends" and "plugins" are relative to. Only necessary if these values are relative paths. More info.

--config

配置文件 的路径,支持 JSON、YAML 或 JS 格式。详细信息见这里

--custom-syntax

指定你的代码中所包含的自定义语法。详细信息见这里

--disable-default-ignores, --di

禁用Disable the default ignores. Stylelint will not automatically ignore the contents of node_modules. More info.

--fix

Automatically fix, where possible, problems reported by rules. More info.

--formatter, -f | --custom-formatter

Specify the formatter to format your results. More info.

--ignore-disables, --id

Ignore stylelint-disable (e.g. /* stylelint-disable block-no-empty */) comments. More info.

--ignore-path, -i

A path to a file containing patterns describing files to ignore. The path can be absolute or relative to process.cwd(). By default, Stylelint looks for .stylelintignore in process.cwd(). More info.

--ignore-pattern, --ip

Pattern of files to ignore (in addition to those in .stylelintignore).

--max-warnings, --mw

Set a limit to the number of warnings accepted. More info.

--output-file, -o

Path of file to write a report. Stylelint outputs the report to the specified filename in addition to the standard output.

--print-config

Print the configuration for the given path. Stylelint outputs the configuration used for the file passed.

--quiet, -q

Only register problems for rules with an "error"-level severity (ignore "warning"-level). More info.

--report-descriptionless-disables, --rdd

Produce a report of the stylelint-disable comments without a description. More info.

--report-invalid-scope-disables, --risd

Produce a report of the stylelint-disable comments that used for rules that don't exist within the configuration object. More info.

--report-needless-disables, --rd

Produce a report to clean up your codebase, keeping only the stylelint-disable comments that serve a purpose. More info.

--stdin-filename

A filename to assign the input. More info.

--stdin

Accept stdin input even if it is empty.

--version, -v

Show the currently installed version of Stylelint.

Usage examples

The CLI expects input as either a file glob or process.stdin. It outputs formatted results into process.stdout.

Be sure to include quotation marks around file globs.

Example A - recursive

Recursively linting all .css files in the foo directory:

stylelint "foo/**/*.css"

Example B - multiple file extensions

Linting all .css, .scss, and .sass files:

stylelint "**/*.{css,scss,sass}"

Example C - stdin

Linting stdin:

echo "a { color: pink; }" | stylelint

Example D - negation

Linting all .css files except those within docker subfolders, using negation in the input glob:

stylelint "**/*.css" "!**/docker/**"

Example E - caching

Caching processed .scss files foo directory:

stylelint "foo/**/*.scss" --cache --cache-location "/Users/user/.stylelintcache/"

Example F - writing a report

Linting all .css files in the foo directory, then writing the output to myTestReport.txt:

stylelint "foo/*.css" --output-file myTestReport.txt

Example G - specifying a config

Using bar/mySpecialConfig.json as config to lint all .css files in the foo directory and any of its subdirectories:

stylelint "foo/**/*.css" --config bar/mySpecialConfig.json

Example H - using a custom syntax

Recursively linting all .css files in the foo directory using a custom syntax:

stylelint "foo/**/*.css" --customSyntax path/to/my-custom-syntax.js

Example I - print on success

Ensure output on successful runs:

stylelint -f verbose "foo/**/*.css"

退出码

命令行工具(CLI)进程退出时会返回以下退出码(exit codes):

  • 1 - 未知错误
  • 2 - 至少有一个规则(rule)出了问题或者命令行参数错误
  • 78 - 配置文件有问题