Grunt中的Nodemon-like任务:执行节点进程和监视

我觉得我失去了一些东西。

这是我想要达到的:

有一个咕噜的任务执行我的server.js和并行运行watch任务。 我觉得这正是Gruntdevise的任务之一,但我无法实现这种configuration。

其中,我读过这个: 通过Grunt运行Node应用程序,但我仍然无法做到。

这是我的Gruntfile.js:

 module.exports = function(grunt) { // Project configuration. grunt.initConfig({ watch: { scripts: { files: ['*.js'], tasks: ['start'], options: { nospawn: true } } } }); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.registerTask('start', function() { grunt.util.spawn({ cmd: 'node', args: ['server.js'] }); grunt.task.run('watch'); }); grunt.registerTask('default', 'start'); }; 

我有"grunt-contrib-watch": "~0.3.1" grunt-contrib-watch@0.3.0 "grunt-contrib-watch": "~0.3.1" ,应该比前面提到的文章更高版本,比grunt-contrib-watch@0.3.0

如果你能帮我实现适当的configuration,我将非常感激。 但更一般的说,我不明白为什么没有官方的grunt-contrib-nodemon-like软件包和任务,因为我感觉这将是使用grunt(我真的很喜欢这个工具!

谢谢

编辑: grunt-nodemon

自写这个,一个很好的人开发了这个。


使用grunt.util.spawn来激发新进程时遇到了很多麻烦。 他们会跑,但他们不会给我任何回报。 也许你可以弄清楚我在这些文档中不能做什么。 http://gruntjs.com/api/grunt.util#grunt.util.spawn

我看到你有什么两个问题:

  • 我想grunt.registerTask()在使用callback函数来运行你的任务时必须带三个参数。
  • 我不认为每次文件更改时都可以一次又一次地调用node server.js 这将是第一次,因为它真的工作,你必须作为一个subprocesspipe理服务器,杀死并重新启动后续的文件更改。

对于registerTask参数试试这个,只是为了看看你能在当前的实现中得到一些东西。

http://gruntjs.com/api/grunt.task#grunt.task.registertask

它需要(taskName, description, taskFunction)如下所示:

 grunt.registerTask('start', 'My start task description', function() { grunt.util.spawn({ cmd: 'node', args: ['server.js'] }); grunt.task.run('watch'); }); 

这可能至less让你的watch在第一次文件改变时运行node server.js

这是我会做的。

要么就像使用nodemon $ nodemon server.js一样

要么…

阅读源代码并使用grunt-develop

他正在pipe理服务器作为一个subprocess,可能是你在找什么。

要么…

得到grunt-shell
npm install grunt-shell --save-dev

并用它来为你运行nodemon:

 module.exports = function(grunt) { // Project configuration. grunt.initConfig({ serverFile: 'server.js', shell: { nodemon: { command: 'nodemon <%= serverFile %>', options: { stdout: true, stderr: true } } }, watch: { /* nothing to do in watch anymore */ } }); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-shell'); grunt.registerTask('default', 'shell:nodemon'); }; 

$ grunt shell:nodemon

我衷心希望有所帮助。 祝你好运!

嗨,我也遇到了这个问题,这里是我的解决scheme(根据nackjicholson的答案)。 这在产生的进程中使用grunt-nodemon 。 所以我可以:

  • 重新加载nodejs
  • 注意对无文件的更改
  • 获取这两个任务的输出

     grunt.loadNpmTasks('grunt-nodemon'); grunt.initConfig({ nodemon: { dev: { options: { file: 'server.js', nodeArgs: ['--debug'], env: { PORT: '8282' } } } }, }); grunt.registerTask('server', function (target) { // Running nodejs in a different process and displaying output on the main console var nodemon = grunt.util.spawn({ cmd: 'grunt', grunt: true, args: 'nodemon' }); nodemon.stdout.pipe(process.stdout); nodemon.stderr.pipe(process.stderr); // here you can run other tasks eg // grunt.task.run([ 'watch' ]); }); 

使用grunt-concurrent

问题是像watch和nodemon这样的任务永远不会终止,所以咕噜声永远不会到达它们。 你需要产生一个新的过程。

你可以使用grunt-concurrent轻松地做到这一点:

https://github.com/sindresorhus/grunt-concurrent

例如:

 module.exports = function(grunt) { grunt.initConfig({ ... concurrent: { dev: { tasks: ['nodemon', 'watch'], options: { logConcurrentOutput: true } } } }); }; 

两人现在快乐地并肩奔跑。