通过Grunt运行Node应用程序 [英] Running Node app through Grunt

查看:101
本文介绍了通过Grunt运行Node应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行我的Node应用程序作为一个Grunt任务。我需要将它作为一个子进程产生,然而,允许我并行运行watch任务。

这个工作原理:

  grunt.registerTask('start',function(){
grunt.util.spawn(
{cmd:'node'
,args:['app.js']
})

grunt.task.run('watch:app')
})

但是,当监视任务检测到更改时,这将再次触发启动任务。在我生成Node应用程序的另一个子进程之前,我需要杀死前一个进程。



但是,我无法弄清楚如何杀死进程。像这样的东西不起作用:

  var child 

grunt.registerTask('start',function (){
if(child)child.kill()
child = grunt.util.spawn(
{cmd:'node'
,args:['app.js ']
})

grunt.task.run('watch:app')
})

看起来:


  1. 即使我将生成的进程存储在外部变量中它不会持久化,所以下次运行启动任务时,child是 undefined

  2. child 没有 kill 函数...


解决方案

这是因为 grunt-contrib-watch 目前会将所有任务运行为子进程。所以变量 child 不在同一个进程上下文中。很快, grunt-contrib-watch@0.3.0 将会以 nospawn 选项发布。这将使您可以配置手表在相同的上下文中产生任务运行,并让您的上例工作。



请看看这个问题,以获取更多信息:



https://github.com/gruntjs/grunt-contrib-watch/issues/45


I am trying to run my Node application as a Grunt task. I need to spawn this as a child process, however, to allow me to run the watch task in parallel.

This works:

grunt.registerTask('start', function () {
  grunt.util.spawn(
    { cmd: 'node'
    , args: ['app.js']
    })

  grunt.task.run('watch:app')
})

However, when changes are detected by the watch task, this will trigger the start task again. Before I spawn another child process of my Node app, I need to kill the previous one.

I can't figure out how to kill the process, however. Something like this does not work:

var child

grunt.registerTask('start', function () {
  if (child) child.kill()
  child = grunt.util.spawn(
    { cmd: 'node'
    , args: ['app.js']
    })

  grunt.task.run('watch:app')
})

It appears that:

  1. Even though I store the spawned process in a variable outside of the function context, it does not persist, so the next time the start task is run, child is undefined.
  2. child has no kill function…

解决方案

This is because grunt-contrib-watch currently spawns all task runs as child processes. So the variable child is not within the same process context. Fairly soon, grunt-contrib-watch@0.3.0 will be released with a nospawn option. This will let you configure the watch to spawn task runs within the same context and would make your above example work.

Take a look at this issue for a little more information:

https://github.com/gruntjs/grunt-contrib-watch/issues/45

这篇关于通过Grunt运行Node应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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