node.js grunt watch 任务阻塞命令行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15577905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
grunt watch task block the command line
提问by Ivan V.
I'm new to grunt, still learning, so I came up to a very strange problem.
When I run "watch"task, my command line becomes blocked, so basically I cant do anything on it.
Bare in mind that the task is completed with success.
This is my command line output:
我是 grunt 的新手,还在学习中,所以我遇到了一个非常奇怪的问题。当我运行"watch"任务时,我的命令行被阻塞,所以基本上我不能对它做任何事情。请记住,任务已成功完成。这是我的命令行输出:
C:\server\css-test>grunt w
Running "watch" task
Waiting...OK
>> File "compass-examples-master\sass\screen.scss" changed.
Running "compass" (compass) task
unchanged compass-examples-master/02/sass/ie.scss
unchanged compass-examples-master/02/sass/print.scss
overwrite compass-examples-master/02/stylesheets/new/sass/screen.css
Running "watch" task
Completed in 1.496s at Fri Mar 22 2013 19:31:37 GMT+0100 (Central Europe Standard Time) - Waiting...
As you can see, all I do is run "compass"task, it completes successfully.
如您所见,我所做的只是运行"compass"任务,它成功完成。
Insertion point keeps blinking on after the Waiting...text part, but keyboard input doesn't work.
插入点在Waiting...文本部分后一直闪烁,但键盘输入不起作用。
My grunt configuration
我的咕噜配置
module.exports = function (grunt)
{
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
compass: {
dist: {
options: {
sassDir: 'compass-examples-master/02',
cssDir: 'compass-examples-master/02/stylesheets/new',
imagesDir: 'compas-examples-master/02/images',
boring: false,
outputStyle: 'nested',
require: 'sass-media_query_combiner'
}
}
},
watch: {
files: 'compass-examples-master/02/sass/*',
tasks: ['c']
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-compass');
grunt.registerTask('c', 'compass');
grunt.registerTask('w', 'watch');
};
回答by Nick Mitchinson
I havn't used grunt watch, however I believe the problem is that it is a continuously running process, and by default your command line will typically run one process at a time. There are 3 solutions you can use.
我没有使用过 grunt watch,但是我认为问题在于它是一个持续运行的进程,默认情况下,您的命令行通常一次运行一个进程。您可以使用 3 种解决方案。
- Open a new terminal. You can have multiple open at once, and this will allow you to use the second as your main terminal while grunt watch occupies the other.
Use the & symbol in your command. This will begin the process and immediately return the command line to you, allowing you to use it for other things. You can think of it as running the command in the background. The process is still connected to the terminal window, so if you close the window, the process will end.
grunt w &
Use ( &) around your command. This will run in the background the same as just &, however the process will not be killed when you close the terminal window.
(grunt w &)
- 打开一个新终端。您可以一次打开多个,这将允许您将第二个用作主终端,而 grunt watch 占用另一个。
在命令中使用 & 符号。这将开始该过程并立即将命令行返回给您,允许您将其用于其他用途。您可以将其视为在后台运行命令。该进程仍然连接到终端窗口,因此如果关闭该窗口,该进程将结束。
咕噜咕噜
在命令周围使用 (&)。这将在后台运行,就像 & 一样,但是当您关闭终端窗口时,该进程不会被终止。
(咕噜声&)
回答by Christian Matthew
For Windows use
对于 Windows 使用
start grunt
This will bring a CMD window that will "watch" in the background and leave the git-bash or cmd window you were working in the same as you left it.
这将带来一个 CMD 窗口,该窗口将在后台“观察”,并使您工作的 git-bash 或 cmd 窗口与您离开时相同。
In other words, this will run the task in a separate cmd window for you.
换句话说,这将为您在单独的 cmd 窗口中运行任务。
Just wanted to let people know this worked per the comments.
只是想让人们知道根据评论这是有效的。
回答by jojojohn
I find it simpler to just stop the watch process by pressing Ctrl and c at the same time. Then I can use the command line then to do stuff then run grunt watch again when Im ready to go back to the browser.
我发现通过同时按 Ctrl 和 c 来停止监视过程更简单。然后我可以使用命令行然后做一些事情,然后当我准备好返回浏览器时再次运行 grunt watch 。
回答by Ian Jamieson
I use screen, https://www.gnu.org/software/screen/, so I can switch between terminal windows.
我使用 screen, https://www.gnu.org/software/screen/,所以我可以在终端窗口之间切换。

