node.jsprocessgulppipe

Format output of spawned gulp process like the parent process


Usecase: On large projects it can be nice to separate small projects into folders of their own with their own build process.

The following setup basically works on both windows and mac - I get the output of the child gulp process logged in the console - only problem is that it's not colored like the output of the parent process.

var spawnCmd = require('spawn-cmd');

gulp.task('default', function () {
    // Run all the parent projects tasks first
    // ....
    // ....
    // ....

    // When done, cd to child directory
    process.chdir('./some-dir-that-has-a-gulpfile');

    // Run `gulp` in the child directory
    var child = spawnCmd.spawn('gulp', ['default']);

    // And pipe the output to the current process
    child.stdout.pipe(process.stdout);
});

My question is how to display the output of the child gulp process in exactly the same way as the normal gulp process.

Edit: Duplicate of Is it possible for child processes in Node.js to preserve colored output?


Solution

  • You should inherit the stdio of the parent process. This correctly pipes the output to the same output, with colors and all.

    Because you are using gulp, you should also add the --color always flag, in order for gulp to properly detect that you want colors.

    var spawnCmd = require('spawn-cmd');
    
    gulp.task('default', function () {
        // When done, cd to child directory
        process.chdir('./some-dir-that-has-a-gulpfile');
    
        // Run `gulp` in the child directory
        var child = spawnCmd.spawn('gulp', ['default', '--color', 'always'], {stdio: 'inherit'});
    });