How do you get gulp-exec to print to stdout when the command fails?

I am using the following code to start a process from gulp running on Windows 8:

gulp.src('test.bat')
  .pipe($.exec('test.bat <%= file.path %>'))
  .pipe($.exec.reporter());

      

The batch file in the current folder contains the following:

@echo off

echo This is a test to stdout
echo The script was passed %*

exit 0

      

The output looks like this:

[11:27:40] This is a test to stdout
The script was passed C:\temp\test.bat

      

If the batch file returns -1, the output looks like this:

events.js:85
      throw er; // Unhandled 'error' event
        ^
Error: Command failed: C:\WINDOWS\system32\cmd.exe /s /c "test.bat C:\temp\test.bat"

    at ChildProcess.exithandler (child_process.js:751:12)
    at ChildProcess.emit (events.js:110:17)
    at maybeClose (child_process.js:1015:16)
    at Socket.<anonymous> (child_process.js:1183:11)
    at Socket.emit (events.js:107:17)
    at Pipe.close (net.js:485:12)

      

How do I get gulp.exec () to first print the standard output from the script and secondly make it more graceful? the error thrown from events.js is not something I should expect to see, but rather some nifty command error message. According to the docs, all the default options should already be set to output to stdout and stderr, so not sure what I might be doing wrong here.

+3


source to share


2 answers


gulp-exec

assumes that the command will return 0 for success and non-0 for failure, which means that you want the build to fail. (He has yet to drop stdout

and stderr

the console before it fails Although if these assumptions are not true, chances are you'll avoid gulp -exec and do it together.:



var through2 = require('through2');
var exec = require('child_process').exec;

gulp.task('sometask', function() {
    return gulp.src('**/*.js')
        .pipe(through2.obj(function (file, enc, cb) {
            var that = this;
            exec('test.bat some args here', function (err, stdout, stderr) {
                // take appropriate action then
                that.push(file);
                cb(null);
            });
        })
        .pipe(gulp.dest('dist'));
});

      

+1


source


I had the same problem and could fix the following approach:

gulp.src('test.bat')
    .pipe($.exec('test.bat <%= file.path %>', function cb(err, stdout, stderr) {
        console.log(stdout); // outputs the normal messages
        console.log(stderr); // outputs the error messages
        return 0; // makes gulp continue even if the command failed
    }))
    .pipe($.exec.reporter());

      



The tricky part here is that it exec

provides a callback ( $.exec(command, callback)

) function where we can register exits and bypass the command return by simply returning 0

!

0


source







All Articles