Search code examples
javascriptnode.jsgulpjscs

gulp-jscs seems to be unable to detect the config (.jscsrc) file, but normal jscs can from the command line


When I run "gulp style" from the command line, Gulp runs, and, subsequently, gulp-jscs runs, but the latter seems to be unable to detect the rules defined in the jscs config file (.jscsrc). But, if I run jscs from the command line, then jscs does detect the config file's rules. Any idea what the deal could be?

Here's my gulp file:

(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"}));
    });
})();

Solution

  • You need a reporter (just like jshint has one):

    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
    });
    

    Other notes, (if you are running from cmd), Gulpfile.js you don't need to wrap it into anonymous function or use 'use strict'.

    Example output:

    [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
    

    If you're not sure how will current path ./ be taken into account you can always use path module to resolve, for example:

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