简体   繁体   English

如何将gulp-w3c-css的输出传递到控制台

[英]How can I pipe the output from gulp-w3c-css to the console

When I follow the examples for gulp-w3c-css I am unable to get the results to print in the console instead of in an output directory. 当我按照gulp-w3c-css的示例进行操作时,无法在控制台中而不是在输出目录中打印结果。

Compare how I am using CSSLint and W3C-CSS below. 在下面比较我如何使用CSSLint和W3C-CSS。 I'd like the function to be identical. 我希望功能相同。

var gulp = require('gulp'),
    csslint = require('gulp-csslint'),
    cssvalidate = require('gulp-w3c-css');


gulp.task('csslint', () =>
    gulp.src('testcss/laxhjalpen.css')
    .pipe(csslint('.csslintrc'))
    .pipe(csslint.reporter())
);

// Does not work
gulp.task('cssvalid', () =>
    gulp.src('testcss/*css')
    .pipe(cssvalidate())
    // Next line works but is not what I want
    .pipe(gulp.dest('reports'))
    // I suppose I need to get this construct to work but I can't
    .pipe(gutil.buffer(function(err, files) {
        if (err) {
            gutil.log('An error occured', err);
        } else {
            // No idea what to write
            // files - array of validation results (from manual)
        }
    }))
);

The very best solution would be just to have a reporter function that works like the csslint.reporter does. 最好的解决方案是仅具有与csslint.reporter相同的报告器功能。

The gulp-w3c-css plugin serializes the validation results for each file to JSON and stores that JSON in the file.contents property. gulp-w3c-css插件将每个文件的验证结果序列化为JSON,并将该JSON存储在file.contents属性中。 The format of that JSON serialization looks roughly like the following (for more details see the w3c-css documentation ): JSON序列化的格式大致如下所示(有关更多详细信息,请参见w3c-css文档 ):

{
  errors: [ { line: 5, message: 'Some error' },
            { line: 42, message: 'Some error' } ],
  warnings: [ { line: 13, message: 'Some warning' },
              { line: 23, message: 'Some warning' } ]
}

So all you have to do is parse that JSON and then log the information to the console in any way you want. 因此,您要做的就是解析该JSON,然后以所需的任何方式将信息记录到控制台。

Here's a simple example of how you could do it: 这是一个简单的示例,说明如何实现:

var gulp = require('gulp');
var cssvalidate = require('gulp-w3c-css');
var gutil = require('gulp-util');
var map = require('map-stream');

gulp.task('cssvalid', function () {
  return gulp.src('testcss/*css')
    .pipe(cssvalidate())
    .pipe(map(function(file, done) {
      if (file.contents.length == 0) {
        console.log('Success: ' + file.path);
        console.log(gutil.colors.green('No errors or warnings\n'));
      } else {
        var results = JSON.parse(file.contents.toString());
        results.errors.forEach(function(error) {
          console.log('Error: ' + file.path + ': line ' + error.line);
          console.log(gutil.colors.red(error.message) + '\n');
        });
        results.warnings.forEach(function(warning) {
          console.log('Warning: ' + file.path + ': line ' + warning.line);
          console.log(gutil.colors.yellow(warning.message) + '\n');
        });
      }
      done(null, file);
    }));
});

I used map-stream instead of gutil.buffer() so that the results for each file are printed as soon as they are available instead of printing everything at the very end. 我使用map-stream而不是gutil.buffer()以便每个文件的结果在可用时立即打印,而不是在最后打印所有内容。

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

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