Skip to content

Instantly share code, notes, and snippets.

@rmckeel
Last active August 9, 2021 23:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmckeel/b4e60922f5098ced9c50bdd96731b34a to your computer and use it in GitHub Desktop.
Save rmckeel/b4e60922f5098ced9c50bdd96731b34a to your computer and use it in GitHub Desktop.
Gulp no-dependency spawn / execute a file with live stdout feedback
const gulp = require( 'gulp' );
const spawn = require( 'child_process' ).spawn;
/**
* This solution to execute and watch a shell function from Gulp is
* adapted from https://stackoverflow.com/a/10232330/3232832
*
* e.g. callSpawn( 'ping', [ '-c 5', 'google.com' ], cb );
*/
function callSpawn( command, arguments, cb ) {
const call = spawn( command, arguments );
// stdout
call.stdout.on( 'data', function ( data ) {
// like console.log, but without the trailing newline
process.stdout.write( data.toString() );
} );
// stderr
call.stderr.on( 'data', function ( data ) {
console.log( data.toString() );
cb( data.toString() );
} );
call.on( 'exit', function ( code ) {
console.log( 'child process exited with code ' + code.toString() );
cb();
} );
}
gulp.task( 'test:ping', function ( cb ) {
callSpawn( 'ping', [ '-c 10', 'google.com' ], cb );
} )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment