2016-03-15 81 views
1

我正在尝试在我的项目中首次设置jshint linter。我正在使用gulp-jshint。以下配置工作得很好我的每个文件都有错误数量。 问题:是否有可能计算项目中的所有错误?使用Gulp显示错误总数Jshint

即:如果的fileA有4个错误,FILEB有2个错误,fileC有1个错误 你最终的东西吞气任务“您的项目总共有7错误”

这是我大口的配置

gulp.task('lint', function() { 
    return gulp.src(paths.scripts) 
    .pipe(jshint('.jshintrc')) 
    .pipe(jshint.reporter('jshint-stylish')); 
}); 

谢谢!

回答

3

编辑:由于我发布了这个答案,我已经清理并显着扩展了我在下面写的代码,并将它作为自己的包发布在npm:jshint-stylish-summary上。

下面是一个输出的截图:

 


jshint-stylish似乎不支持最后的总结。你可能想要寻找一个不同的记者,做你想做的事。如果你想使用jshint-stylish保持(快速perusal of mine没有屈服但是任何有用的东西。)

,你还必须添加a custom reporter的选项。这里有一个我实施了应该让你开始:

var map = require('map-stream'); 

var jshintTotalReporter = function() { 
    var total = { warnings: 0, errors: 0 }; 
    total.files = { all: 0, success: 0, warning: 0, error: 0 }; 
    return { 
    collect: map(function(file, cb) { 
     var codes = { W: 0, E: 0 }; 
     (file.jshint.results||[]).forEach(function(result) { 
     codes[result.error.code[0]]++; 
     }); 
     total.warnings += codes['W']; 
     total.errors += codes['E']; 
     total.files.all++; 
     total.files.success += (codes['W'] + codes['E'] > 0) ? 0 : 1; 
     total.files.warning += (codes['W'] > 0) ? 1 : 0; 
     total.files.error += (codes['E'] > 0) ? 1 : 0; 
     cb(); 
    }), 
    report: function() { 
     console.log(total.files.all + ' files checked'); 
     console.log(total.files.success + ' files without problems'); 
     console.log(total.files.warning + ' files with warnings (' + 
        total.warnings + ' warnings total)'); 
     console.log(total.files.error + ' files with errors (' + 
        total.errors + ' errors total)'); 
    } 
    }; 
}; 

gulp.task('lint', function() { 
    var jshintTotals = jshintTotalReporter();  
    return gulp.src(paths.scripts) 
     .pipe(jshint('.jshintrc')) 
     .pipe(jshint.reporter('jshint-stylish')) 
     .pipe(jshintTotals.collect) 
     .on('end', jshintTotals.report); 
}); 

(不要忘了运行npm install --save-dev map-stream或这将无法工作)。

输出相当简陋,但应该包含所有可能感兴趣的信息。例如:

9 files checked 
3 files without problems 
6 files with warnings (13 warnings total) 
4 files with errors (7 errors total) 

如果要漂亮的东西,你可以使用chalk一些颜色添加到输出和log-symbolsjshint-stylish用途可爱的小图标。