running gulp tasks that include shell processes synchronously -


i trying use gulp installer complex system involves creating folder, copying files around , runnin compliation scripts.

presently have following gulp tasks:

 // tasks skipped set sessionfolder   gulp.task('default', function () { // main       runsequence('prepare_comedi', 'compile_comedi'); });   gulp.task('prepare_comedi', function () {  //  copies comedi files build folder      gulp.src(['../comedi/**/*']).pipe(gulp.dest(sessionfolder)); });    gulp.task('compile_comedi', function () {         var logfile=this.currenttask.name+'.log';        gutil.log(gutil.colors.green(this.currenttask.name), ": building , installing comedi, logging "+logfile);     var cmd= new run.command('{ ./autogen.sh; ./configure; make; make install; depmod -a ; make dev;} > ../'+logfile+ ' 2>&1', {cwd:sessionfolder+'/comedi', verbosity:3});    cmd.exec(); }); 

when run gulp, becomes obvious processes start in background , gulp task finishes immediately. first task above should copy source files, , second 1 compile them. in practice, second task hits error, first task not ready copying when second task (almost immediately) starts.

if run second task alone, previosuly having files first task copied, works ok, have output this:

 [19:52:47] starting 'compile_comedi'...  [19:52:47] compile_comedi : building , installing comedi, logging compile_comedi.log   $ { ./autogen.sh; ./configure; make; make install; depmod -a ; make dev;} > ../compile_comedi.log 2>&1  [19:52:47] finished 'compile_comedi' after 6.68 ms 

so takes 6.68 millisec leave task, while want gulp leave after compilations specified in task finished. run compile process uses built binaries step dependency.

how can run external commands in such way, next gulp task starts after first task complete execution of external process?

you should make sure task prepare_comedi finalized prior start compile_comedi. in order so, since you're using regular streams on prepare task, return stream:

gulp.task('prepare_comedi', function () {   // !!! returns stream. gulp not consider task done   // until stream ends.   return gulp.src(['../comedi/**/*']).pipe(gulp.dest(sessionfolder)); }); 

since these tasks interdependent , require order, might want consider refactoring code create 2 methods , call them normally. take @ note.

update

addressing question in comment below, if want hold task until asynchronous job has been completed, have pretty 3 choices:

  1. return stream (case above)
  2. returning promise , fulfilling when you're done (using q in example):
var q = require('q');  gulp.task('asyncwithpromise', function() {   var deferred = q.defer();    // asynchronous   settimeout(function() {     q.resolve('nice');   }, 5000);    return deferred.promise; }); 
  1. receiving callback function , calling it
gulp.task('asyncwithpromise', function(done) {   settimeout(function() {     done();   }, 5000); }); 

these approaches in docs.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -