node.js 如何将 gulp 结果输出到控制台?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24468310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:33:59  来源:igfitidea点击:

How do I output gulp results to console?

node.jsgulpgulp-spellcheck

提问by David Silva Smith

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.

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

Instead I get an error:

相反,我收到一个错误:

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

Here is my code

这是我的代码

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 '\n' + match + '\n';
      }
      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);
});

回答by leogdion

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

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

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

Implemented in code:

在代码中实现:

 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 '\n' + match +'\n'; 
            } 
            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 = '\n\n' + Array(bufLength).join("_") + '\n\n'
        if (contents.length > 1) {
            process.stdout.write(chunk.path + '\n' + contents + '\n');
            process.stdout.write(chunk.path + hr);
        }
    });
});