在进程启动时用回调启动一个Gulp中的shell进程 [英] Start a shell process in Gulp with callback when process started

查看:90
本文介绍了在进程启动时用回调启动一个Gulp中的shell进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图依次在Gulp上运行一些任务。其中一项任务是执行简单的 $ node app.js 的shell脚本。



tl; dr


如何启动回叫,以便我可以告诉Gulp服务器已启动?



因此,下面是我想要完成的大图:

我是使用gulp 运行序列顺序触发多项任务,该任务指定了一些方法,您应该编写你的任务,以便它们按顺序运行。



每个 gulp.task()都必须:




  • 返回

  • 在任务中调用回调函数



我的设置:




  • gulp.task(clean,..); //返回流,全部OK

  • gulp.task(compile,..); //返回流,全部OK

  • gulp.task(spin-server,..); //使用hack调用回调

  • gulp.task(init-browser-sync,..); //最后一项任务



这是我的 spin-server 任务:

  gulp.task(spin-server,function(cb){
exec( sh shell-utilities / spin-server);

// @HACK允许服务器在`runSequence`
// //运行下一个任务之前启动
setTimeout (function(){
cb();
},2500);
});

以下是 spin-server.sh shell脚本:

  ##开始节点服务器## 

节点服务器/ app.js

#######
#EOF#
#######



问题



现在我正在使用 setTimeout hack请确保在继续运行 init-browser-sync 任务之前,我的Node / Express服务器已经启动。



当我的Express服务器实际启动时,如何消除 setTimeout hack并调用 cb()

解决方案

如果您希望产生一个进程但听取输出结果,可以使用 exec 然后将侦听器附加到进程的stdout。

  var exec = require('child_process')。 EXEC; 
$ b gulp.task(spin-server,function(){

var child = exec(sh shell-utilities / spin-server);

child.stdout.on('data',function(data){
console.log('stdout:'+ data);
});
child.stderr .on('data',function(data){
console.log('stderr:'+ data);
});
child.on('close',function(code ){
console.log('closing code:'+ code);
});
});


I'm trying to run some tasks in Gulp, sequentially. One of those tasks is a shell script that performs a simple $ node app.js. How can I fire the callback so I can tell Gulp that the server has started?

tl;dr


So here's the bigger picture of what I'm trying to accomplish:

I'm using gulp run-sequence to fire up a number of tasks sequentially, which specifies a couple ways you should be writing your tasks in order for them to be run in sequence.

Each gulp.task() must either:

  • return the stream or
  • call a callback on the task

My setup:

  • gulp.task("clean", ..); // returns the stream, all OK
  • gulp.task("compile", ..); // returns the stream, all OK
  • gulp.task("spin-server", ..); // calls the callback with a hack
  • gulp.task("init-browser-sync", ..); // last task

Here's my spin-server task:

gulp.task("spin-server", function(cb) {
  exec("sh shell-utilities/spin-server");

  // @HACK allow time for the server to start before `runSequence`
  // runs the next task.
  setTimeout(function() {
    cb();
  }, 2500);
});

And here's the spin-server.sh shell script:

## Start Node server ##

node server/app.js

#######
# EOF #
#######

The problem

Right now I'm using a setTimeout hack to make sure my Node/Express server has fired up before proceeding to run the init-browser-sync task.

How can I eliminate that setTimeout hack and call cb() when my Express server actually fires up?

解决方案

If you'd like to spawn a process but listen to it's output, you can start it with exec and then attach listeners to the stdout of the process.

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

gulp.task("spin-server", function() {

  var child = exec("sh shell-utilities/spin-server");

  child.stdout.on('data', function(data) {
    console.log('stdout: ' + data);
  });
  child.stderr.on('data', function(data) {
    console.log('stderr: ' + data);
  });
  child.on('close', function(code) {
    console.log('closing code: ' + code);
  });
});

这篇关于在进程启动时用回调启动一个Gulp中的shell进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆