Grunt中的Nodemon-like任务:执行节点进程和监视 [英] Nodemon-like task in Grunt : execute node process and watch
问题描述
这是我想达到的目的:
有一个执行我的 server.js
并且并行运行 watch
任务的咕task任务。我感觉这正是grunt设计的任务之一,但我无法实现这种配置。
其中,我读过这个:
通过Grunt运行Node应用程序
但我仍无法制作它是。
这是我的Gruntfile.js:
模块。 exports =函数(grunt){
//项目配置。
grunt.initConfig({
watch:{
scripts:{
files:['* .js'],
tasks:['start'],
选项:{
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-nodemon-like
包和任务,因为我觉得这将是另一个很好的理由使用grunt(我真的很喜欢这个工具!)
谢谢
编辑: grunt-nodemon
自写这篇文章以来,一个很好的开发人员。
我在使用grunt.util.spawn来引发新的问题时遇到了很多麻烦流程。他们会跑,但他们不会给我任何回报。也许你可以弄清楚我在这些文档中不能做什么。 http://gruntjs.com/api/grunt.util#grunt.util.spawn
我看到的有两个问题:
- 我认为grunt.registerTask()在使用回调函数运行任务时必须带三个参数。
- 我不认为每次文件更改时都可以一次又一次地调用
node server.js
。它会第一次工作,因为它真的起作用,所以你必须将服务器作为子进程来管理,在随后的文件更改中杀死并重新启动它。
对于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');
});
至少可以让 watch
为在文件首次更改时运行 node server.js
。
这就是我会做的。
无论是使用nodemon $ nodemon server.js
原样
或...
阅读资料来源并使用 grunt-develop
<
或... b
获取 grunt-shell
npm install grunt- shell --save-dev
使用它为你运行nodemon:
module.exports = function(grunt){
//项目配置。
grunt.initConfig({
serverFile:'server.js',
shell:{
nodemon:{
command:'nodemon<%= serverFile%> ;',
选项:{
stdout:true,
stderr:true
}
}
},
watch:{/ *没有什么可做的* /}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-shell');
grunt.registerTask('default','shell:nodemon');
};
$ grunt shell:nodemon
我衷心希望有所帮助。祝你好运!
I feel like I'm missing something.
Here is what I want to achieve :
Having a grunt task that executes my server.js
and runs watch
task in parallel. It feels to me that this is precisely one of the tasks grunt was designed for but I can't achieve this configuration.
Among others, I have read this : Running Node app through Grunt but I still can't make it.
Here is my 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');
};
I have "grunt-contrib-watch": "~0.3.1"
which should be higher version than grunt-contrib-watch@0.3.0
as in the previously mentioned post.
If you could help me achieve the proper configuration, I would be extremely grateful. But more in general, I don't understand why there is no official grunt-contrib-nodemon-like
package and task since I have the feeling it would be another great reason to use grunt (which I really like as a tool !)
Thanks
Edit: grunt-nodemon
since writing this, a nice person developed that.
I was having a lot of trouble using grunt.util.spawn to fire off new processes. They would run, but they wouldn't give me any output back. Perhaps you can figure out what I could not in these docs. http://gruntjs.com/api/grunt.util#grunt.util.spawn
Two problems I see with what you have:
- I think grunt.registerTask() has to take three arguments when you use a callback function to run your task.
- I don't think you can just call
node server.js
over and over again everytime a file changes. It will work on the first time, for it to really work you'd have to manage the server as a child process, killing and restarting it on subsequent file changes.
For the registerTask arguments try this, just to see if you can get something to work in your current implementation.
http://gruntjs.com/api/grunt.task#grunt.task.registertask
It takes (taskName, description, taskFunction)
like so:
grunt.registerTask('start', 'My start task description', function() {
grunt.util.spawn({
cmd: 'node',
args: ['server.js']
});
grunt.task.run('watch');
});
That might at least get your watch
to run node server.js
the first time a file changes.
Here's what I would do instead.
Either just use nodemon $ nodemon server.js
as is
or...
Read the source and use grunt-develop
He is managing the server as a child process, might be what you're looking for.
or...
Get grunt-shell
npm install grunt-shell --save-dev
And use it to run nodemon for you:
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
I sincerely hope that helps. Good luck!
这篇关于Grunt中的Nodemon-like任务:执行节点进程和监视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!