How do I output gulp results to console?

2019-02-05 22:55发布

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.

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

1条回答
We Are One
2楼-- · 2019-02-05 23:16

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 event to 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);
        }
    });
});
查看更多
登录 后发表回答