【问题标题】:How to show error console log for stylelint Node API?如何显示 stylelint Node API 的错误控制台日志?
【发布时间】:2020-02-05 20:56:55
【问题描述】:

当在 CLI 上使用 stylelint(如 stylelint "**/_main.scss")并且文件中有错误时,它会出错并记录类似 的内容

但是,当使用Node API 时,日志只是一个带有一些键值的输出,错误是字符串值。就像使用 CLI 时一样,如何使输出出错? 谢谢。

【问题讨论】:

    标签: stylelint


    【解决方案1】:

    stylelint Node API 的默认格式化程序是 "json",而 stylelint CLI 使用 "string" 格式化程序。

    在使用 Node API 时,您可以使用 formatter 属性来使用 "string" 格式化程序,如下所示:

    var stylelint = require("stylelint");
    
    stylelint
      .lint({
        code: "a { unknown: 0 }",
        config: { rules: { "property-no-unknown": true } },
        formatter: "string"
      })
      .then(function({ output, errored }) {
        console.log(output);
        if (errored) process.exit(2);
      })
      .catch(function(err) {
        console.error(err.stack);
      });
    

    Developer guide documentation 详细说明了返回的承诺的结构。您可以使用output 显示格式化程序的结果,使用errored 设置退出代码。

    【讨论】:

    • 不幸的是,这似乎不会产生与 CLI 相同的结果。它不会出错,只显示以下输出:imgur.com/a/Yp5u8Ag
    • 我已更新答案以更正输出并显示如何更改退出代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    相关资源
    最近更新 更多