Skip to content

Ability to read the stdout buffer line-by-line #277

@nathanbrauer

Description

@nathanbrauer

For example, with exec('...', { silent: false }); within a gulp stream, you'll get this:

nathanbrauer@macthan:~/Web/git/Component-Core$ gulp compile:styles
[18:33:08] Using gulpfile ~/Web/git/Component-Core/gulpfile.js
[18:33:08] Starting 'compile:styles'...
    write build/brand.css (0.713s)

    write build/brand.css.map

[18:33:10] Finished 'compile:styles' after 1.17 s

Ideally, each of the lines (starting with write build/brand.css...) would also conform to gulp's logging (gutils.log(...)). But the only way to do that, currently, is to wait for the entire buffer to end, then parse every line on your own. This means that the timestamps are completely wrong which makes it pointless... (but at least it's prettier 😉):

var output = exec('...', { silent: true }).output,
    output_lines = output.replace(/^\s+|\s+$/,'').split(/[\n\r]+/),// Trims the output and returns it split into an array of lines
    log_line;
if (output) {
    for (log_line = 0; log_line < output_lines.length; ++log_line) {
        gutil.log(log_indent,output_lines[log_line]);
    }
}

Here's a real example: https://github.com/Marketera/gulp-composer/blob/master/index.js#L28

Ideally we'd be able to do something along the lines of....

var cmd = exec('composer install', {
    silent: true,
    onPrint: function(line){
        gutil.log(line);
    }
});

// ...which is basically the same as:
var cmd = exec('composer install', {
    silent: true,
    onPrint: gutil.log
});

// ...the default behavior, essentially being:
var cmd = exec('composer install', {
    //...
    onPrint: console.log
});

Some kind of way to "CTRL+C" should also be included...

var cmd = exec('composer install', {
    silent: true,
    onPrint: function(line){
        if (is_something_bad(line)) {
          stream.emit('error', new gutil.PluginError('exec', "I didn't like this line: " + line));
        }
        gutil.log(line);
    }
});

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions