如何将 gulp 结果输出到控制台? [英] How do I output gulp results to console?

查看:20
本文介绍了如何将 gulp 结果输出到控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将拼写检查结果输出到控制台而不是文件,我认为这应该可以工作,因为据我了解它 gulp 返回一个流.

I want to put my spellcheck results out to the console instead of to a file and I think this should work since as I understand it gulp returns a stream.

我得到一个错误:

TypeError: Object #<Stream> has no method 'read'

这是我的代码

gulp.task('spellcheck', function() {
  var patterns = [{
    // Strip tags from HTML
    pattern: /(<([^>]+)>)/ig,
    replacement: ''
  }];
  var spellSuggestions = [{
    pattern: / [^ ]+? (suggestions:[A-z, ']+)/g,
    replacement: function(match) {
      return '<<<' + match + '>>>';
    }
  }];

  var nonSuggestions = [{
    pattern: /<<<.+>>>|([^s]+[^<]+)/g,
    replacement: function(match) {
      if (match.indexOf('<') == 0) {
        return '
' + match + '
';
      }
      return '';
    }
  }];
  var toConsole = gulp.src('./_site/**/*.html')
    .pipe(frep(patterns))
    .pipe(spellcheck())
    .pipe(frep((spellSuggestions)))
    .pipe(frep((nonSuggestions)));
  var b = toConsole.read();
  console.log(b);
});

推荐答案

流上没有读取方法.你有两个选择:

There is no read method on a stream. You have have two choices:

  1. 使用实际的控制台流:process.stdout
  2. 使用数据事件到console.log.
  1. Use the actual console stream: process.stdout
  2. Use the the data event to console.log.

在代码中实现:

 gulp.task('spellcheck', function () {
    var patterns = [
      {
        // Strip tags from HTML
        pattern: /(<([^>]+)>)/ig,
        replacement: ''
      }];

    var nonSuggestions = [
    {
        pattern:  /<<<.+>>>|([^s]+[^<]+)/g,
        replacement: function(match) {
            if (match.indexOf('<')==0) {
                return '
' + match +'
'; 
            } 
            return '';
        }
      }];
    var a = gulp.src('./_site/**/*.html')
        .pipe(frep(patterns))
        .pipe(spellcheck(({replacement: '<<<%s (suggestions: %s)>>>'})))
        .pipe(frep(nonSuggestions))
        ;   

    a.on('data', function(chunk) {
        var contents = chunk.contents.toString().trim(); 
        var bufLength = process.stdout.columns;
        var hr = '

' + Array(bufLength).join("_") + '

'
        if (contents.length > 1) {
            process.stdout.write(chunk.path + '
' + contents + '
');
            process.stdout.write(chunk.path + hr);
        }
    });
});

这篇关于如何将 gulp 结果输出到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆