Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

node-api.md 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Node.js API
  2. The stylelint module includes a `lint()` function that provides the Node.js API.
  3. ```js
  4. stylelint.lint(options).then(function (resultObject) {
  5. /* .. */
  6. });
  7. ```
  8. ## Options
  9. In addition to the [standard options](options.md), the Node API accepts:
  10. ### `config`
  11. A [configuration object](../configure.md).
  12. stylelint does not bother looking for a `.stylelintrc` file if you use this option.
  13. ### `configOverrides`
  14. A partial stylelint configuration object whose properties override the existing config object, whether stylelint loads the config via the `config` option or a `.stylelintrc` file.
  15. ### `code`
  16. A string to lint.
  17. ### `files`
  18. A file glob, or array of [file globs](https://github.com/sindresorhus/globby).
  19. Relative globs are considered relative to `globbyOptions.cwd`.
  20. Though both `files` and `code` are "optional", you _must_ have one and _cannot_ have both.
  21. ### `globbyOptions`
  22. The options that are passed with `files`.
  23. For example, you can set a specific `cwd` manually. Relative globs in `files` are considered relative to this path. And by default, `cwd` will be set by `process.cwd()`.
  24. For more detail usage, see [Globby Guide](https://github.com/sindresorhus/globby#options).
  25. ## The returned promise
  26. `stylelint.lint()` returns a Promise that resolves with an object containing the following properties:
  27. ### `errored`
  28. Boolean. If `true`, at least one rule with an "error"-level severity registered a violation.
  29. ### `output`
  30. A string displaying the formatted violations (using the default formatter or whichever you passed).
  31. ### `postcssResults`
  32. An array containing all the accumulated [PostCSS LazyResults](https://api.postcss.org/LazyResult.html).
  33. ### `results`
  34. An array containing all the stylelint result objects (the objects that formatters consume).
  35. ### `maxWarningsExceeded`
  36. An object containing the maximum number of warnings and the amount found, e.g. `{ maxWarnings: 0, foundWarnings: 12 }`.
  37. ## Syntax errors
  38. `stylelint.lint()` does not reject the Promise when your CSS contains syntax errors.
  39. It resolves with an object (see [The returned promise](#the-returned-promise)) that contains information about the syntax error.
  40. ## Usage examples
  41. ### Example A
  42. As `config` contains no relative paths for `extends` or `plugins`, you do not have to use `configBasedir`:
  43. ```js
  44. stylelint
  45. .lint({
  46. config: { rules: "color-no-invalid-hex" },
  47. files: "all/my/stylesheets/*.css"
  48. })
  49. .then(function (data) {
  50. // do things with data.output, data.errored,
  51. // and data.results
  52. })
  53. .catch(function (err) {
  54. // do things with err e.g.
  55. console.error(err.stack);
  56. });
  57. ```
  58. ### Example B
  59. If `myConfig` _does_ contain relative paths for `extends` or `plugins`, you _do_ have to use `configBasedir`:
  60. ```js
  61. stylelint
  62. .lint({
  63. config: myConfig,
  64. configBasedir: path.join(__dirname, "configs"),
  65. files: "all/my/stylesheets/*.css"
  66. })
  67. .then(function () {
  68. /* .. */
  69. });
  70. ```
  71. ### Example C
  72. Using a string instead of a file glob, and the verbose formatter instead of the default JSON:
  73. ```js
  74. stylelint
  75. .lint({
  76. code: "a { color: pink; }",
  77. config: myConfig,
  78. formatter: "verbose"
  79. })
  80. .then(function () {
  81. /* .. */
  82. });
  83. ```
  84. ### Example D
  85. Using your own custom formatter function and parse `.scss` source files:
  86. ```js
  87. stylelint
  88. .lint({
  89. config: myConfig,
  90. files: "all/my/stylesheets/*.scss",
  91. formatter: function (stylelintResults) {
  92. /* .. */
  93. }
  94. })
  95. .then(function () {
  96. /* .. */
  97. });
  98. ```
  99. ### Example E
  100. Using a custom syntax:
  101. ```js
  102. stylelint
  103. .lint({
  104. config: myConfig,
  105. files: "all/my/stylesheets/*.css",
  106. customSyntax: {
  107. parse: (css, opts) => {
  108. /* .. */
  109. },
  110. stringify: (root, builder) => {
  111. /* .. */
  112. }
  113. }
  114. })
  115. .then(function () {
  116. /* .. */
  117. });
  118. ```
  119. Note that the customSyntax option also accepts a string. [Refer to the options documentation for details](./options.md).