【问题标题】:gulp-jscs seems to be unable to detect the config (.jscsrc) file, but normal jscs can from the command linegulp-jscs 似乎无法检测到 config (.jscsrc) 文件,但普通的 jscs 可以从命令行
【发布时间】:2015-12-25 20:56:53
【问题描述】:

当我从命令行运行“gulp style”时,Gulp 运行,随后 gulp-jscs 运行,但后者似乎无法检测到 jscs 配置文件 (.jscsrc) 中定义的规则。但是,如果我从命令行运行 jscs,那么 jscs 确实会检测到配置文件的规则。知道这笔交易可能是什么吗?

这是我的 gulp 文件:

(function() {
    "use strict";

    var gulp = require("gulp");
    var jshint = require("gulp-jshint");
    var jscs = require("gulp-jscs");
    var jsFiles = ["*.js", "src/**/*.js"];

    gulp.task("style", function () {
        console.log("Running the style task.");
        return gulp.src(jsFiles)
            .pipe(jshint())
            .pipe(jshint.reporter("jshint-stylish", {
                verbose: true
            }))
            .pipe(jscs({configPath: "./.jscsrc"}));
    });
})();

【问题讨论】:

  • 'use strict'node.js 环境中不需要。你能告诉我们.jscsrc 吗?

标签: javascript node.js gulp jscs


【解决方案1】:

你需要一个reporter(就像jshint 有一个):

var gulp = require("gulp");
var jshint = require("gulp-jshint");
var jscs = require("gulp-jscs");
var jsFiles = ["*.js", "src/**/*.js"];

gulp.task("style", function () {
    console.log("Running the style task.");
    return gulp.src(jsFiles)
        .pipe(jshint())
        .pipe(jshint.reporter("jshint-stylish", {
            verbose: true
        }))
        .pipe(jscs({configPath: "./.jscsrc"}))
        .pipe(jscs.reporter()); // << this line here
});

其他注意事项,(如果您从cmd 运行),Gulpfile.js 您不需要将其包装到匿名函数中或使用'use strict'

示例输出:

[13:53:30] Using gulpfile C:\del\so\gulpjscs\Gulpfile.js
[13:53:30] Starting 'style'...
Running the style task.
[13:53:31] gulp-debug: Gulpfile.js
[13:53:31] gulp-debug: index.js
[13:53:31] gulp-debug: 2 items
Comments must start with a lowercase letter at C:\del\so\gulpjscs\index.js :
     1 |// Invalid
--------^
     2 |// valid
     3 |


1 code style error found.
[13:53:31] Finished 'style' after 187 ms

如果您不确定如何考虑当前路径./,您可以随时使用path 模块进行解析,例如:

var path = require('path');
var configPath = path.resolve(path.join(__dirname, '.jscsrc'))

【讨论】:

  • 您是对的:实际上只是将输出通过管道传输到 jscs.reporter()。谢谢!至于与路径相关的东西——我满足于不指定路径,因此允许它选择最近的 .jscsrc,在这种情况下,它位于我项目目录的根目录中。至于“使用严格”规则——我补充说,因为 JSHint 对此感到不满。我不知道如何告诉它忽略某些文件。
  • 很高兴为您提供帮助。有可能,看看docs;可能是"node": true,但我不确定。
  • 是的,我想通了。阻力最小的路径似乎是将 gulpfile 的内容放在以下 cmets 之间: /* jshint ignore:start / 和 / jshint ignore:end */.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 2015-12-14
  • 1970-01-01
  • 2017-02-22
  • 2016-03-15
相关资源
最近更新 更多