简体   繁体   English

将参数发送到Gulp中的jshint记者

[英]Send parameters to jshint reporter in Gulp

I have Gulpfile with jshint configured to use jshint-stylish reporter. 我将jshint的Gulpfile配置为使用jshint-stylish记者。 I need to pass option verbose to reporter in order to display warning codes. 我需要将详细选项传递给记者才能显示警告代码。 Is it possible to do it using Gulp? 可以使用Gulp做到吗?

Current my gulpfile.js looks like below: 当前我的gulpfile.js如下所示:

var gulp = require('gulp');
var jshint = require('gulp-jshint');
var compass = require('gulp-compass');
var path = require('path');
require('shelljs/global');

var jsFiles = ['www/js/**/*.js', '!www/js/libraries/**/*.js', 'www/spec/**/*.js', '!www/spec/lib/**/*.js'];
var sassFiles = 'www/sass/*.scss';

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

gulp.task('compass', function () {
    gulp.src(sassFiles)
        .pipe(compass({
            project: path.join(__dirname, 'www'),
            css: 'css',
            sass: 'sass',
            image: 'img',
            font: 'fonts'
        })).on('error', function() {});
});

var phonegapBuild = function (platform) {
    if (!which('phonegap')) {
        console.log('phonegap command not found')
        return 1;
    }
    exec('phonegap local build ' + platform);
};

gulp.task('build:android', ['lint', 'compass'], function () {
    phonegapBuild('android');
});

gulp.task('build:ios', ['lint', 'compass'], function () {
    phonegapBuild('ios');
});

gulp.task('watch', function() {
    gulp.watch(jsFiles, ['lint']);
    gulp.watch(sassFiles, ['compass']);
});

gulp.task('default', ['lint', 'compass']);

Well, this, plus the fact that the output of the stylish reporter is hardly readable on Windows due to the darkness of the blue text, so I have to keep going in an manually changing the colour after installing it, has made me do something about it. 好的,再加上由于蓝色文本的暗淡,时尚的报告器的输出在Windows上几乎不可读,因此安装后我必须继续手动更改颜色,这使我不得不它。 So you should hopefully have more luck with this reporter I've just written: 因此,希望您对我刚刚写的这位记者感到更加幸运:

https://github.com/spiralx/jshint-summary https://github.com/spiralx/jshint-summary

You basically use it like this; 您基本上是这样使用的;

var summary = require('jshint-summary');

// ...

  .pipe(jshint.reporter(summary({
    verbose: true,
    reasonCol: 'cyan,bold',
    codeCol: 'green'
  })

and the summary function will initialise the function passed to JSHint with those settings - see the page on Github for a bit more documentation. 并且summary函数将使用这些设置来初始化传递给JSHint的函数-有关更多文档,请参见Github上的页面。

It's got some very basic tests, and the library's gulpfile.js uses it to show its own JSHint output :) 它有一些非常基本的测试,并且库的gulpfile.js使用它来显示自己的gulpfile.js输出:)

How about using similar technique, as you already did with phonegap? 就像使用phonegap一样,如何使用类似的技术?

var jshint = function (parameter) {
    // todo: define paths with js files, or pass them as parameter too
    exec('jshint ' + paths + ' ' + parameter);
};

Based on https://github.com/wearefractal/gulp-jshint/blob/master/index.js#L99 it appears that gulp-jshint doesn't facilitate passing more than the name to the reporter if you load it with a string. 基于https://github.com/wearefractal/gulp-jshint/blob/master/index.js#L99 ,似乎gulp-jshint如果您将其加载到字符串中,则除了传递给报告者以外的其他名称不方便。 It seems a simple thing to extend though. 扩展似乎很简单。 I'll race you to a pull request. 我将向您提出请求。 :D :d

Alternatively, try something like this: 或者,尝试如下操作:

var stylish = require('jshint-stylish');

// ...

.pipe(jshint.reporter(stylish(opt)));

I'm pretty sure I have the syntax wrong, but this may get you unstuck. 我敢肯定我的语法有误,但这可能会让您感到困惑。

It's annoying, and makes any decent reporter somewhat tricky to use within the existing framework. 这很烦人,任何体面的记者都很难在现有框架中使用。 I've come up with this hack for the Stylish reporter, it's just currently in my gulpfile.js : 我已经为《时尚》杂志的记者提出了这个技巧,它目前在我的gulpfile.js

function wrapStylishReporter(reporterOptions) {
  var reporter = require(stylish).reporter,
    reporterOptions = reporterOptions || {};

  var wrapped = function(results, data, config) {
    var opts = [config, reporterOptions].reduce(function(dest, src) {
      if (src) {
        for (var k in src) {
          dest[k] = src[k];
        }
      }
      return dest;
    }, {});

    reporter(results, data, opts);
  };

  return jshint.reporter(wrapped);
}

And then for the task definition itself: 然后对于任务定义本身:

gulp.task('lint', function() {
  return gulp.src('+(bin|lib)/**/*.js')
    .pipe(jshint())
    .pipe(wrapStylishReporter({ verbose: true }))
    .pipe(jshint.reporter('fail'));
});

Ideally reporters would either be a function that takes an options parameter and returns the reporter function, or a fairly basic class so you could have options as well as state. 理想情况下,报告程序可以是带有options参数并返回报告程序功能的函数,或者是相当基本的类,因此您既可以具有选项也可以具有state。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM