Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I output gulp results to console?

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);
});
like image 974
David Silva Smith Avatar asked Jun 28 '14 15:06

David Silva Smith


People also ask

What is pipe in Gulp?

Gulp plugins are Node Transform Streams that encapsulate common behavior to transform files in a pipeline - often placed between src() and dest() using the . pipe() method. They can change the filename, metadata, or contents of every file that passes through the stream.


1 Answers

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);
        }
    });
});
like image 60
leogdion Avatar answered Oct 02 '22 16:10

leogdion