gulp-jscs似乎无法检测到配置(.jscsrc)文件,但正常的jscs可以从命令行

时间:2015-12-24 21:41:13

标签: javascript node.js gulp jscs

当我跑步" gulp风格"从命令行开始,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"}));
    });
})();

1 个答案:

答案 0 :(得分:2)

您需要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'))