【问题标题】:How do I output gulp results to console?如何将 gulp 结果输出到控制台?
【发布时间】:2014-06-28 15:30:21
【问题描述】:

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

我得到一个错误:

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

【问题讨论】:

    标签: node.js gulp gulp-spellcheck


    【解决方案1】:

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

    1. 使用实际的控制台流:process.stdout
    2. 使用data event 到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 '\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);
            }
        });
    });
    

    【讨论】:

    • 像这样修改了我的代码:process.stdout.write(a + '\n'); 它输出[object Object]
    • 大卫席尔瓦史密斯,做process.stdout.write(JSON.stringify(a) + '\n')
    • 或者只是process.stdout.write(a) 不需要换行。
    • 这在很多场合都很有用,当有些人想知道任何巨大的链式 gulp 管道中发生了什么(特别是:错误)......
    猜你喜欢
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多