在另一个任务之前异步运行 grunt 任务
Run grunt task asynchronously before another task
我有一个gruntfile设置,这样我就可以开发我的本地angularjs前端,同时将所有api请求转发到单独托管在网络上的java中间层。
这很好用,除了服务器的位置每隔几天就会更改一次,并且我必须使用最新的服务器位置不断更新 gruntfile。
最新的服务器位置可以通过转发到正确位置的 URL 缩短服务找到,因此我可以使用这个繁琐的任务/节点.js代码获取它:
grunt.registerTask('setProxyHost', 'Pings the url shortener to get the latest test server', function() {
request('http://urlshortener/devserver', function(error, response, body) {
if (!error) {
var loc = response.request.uri.href;
if (loc.slice(0, 7) === 'http://') {
proxyHost = loc.slice(7, loc.length - 1);
}
}
});
});
当然,这是异步的,当我运行它时,grunt 在请求完成时已经设置了代理。
如何同步运行此 nodejs 请求或阻止 grunt 直到它完成?这只是为了开发,所以欢迎黑客解决方案。
谢谢
编辑:
很好的答案科里,这基本上解决了问题,因为咕噜咕噜现在等待任务完成,然后再继续。最后一个问题是我无法从 initConfig 访问该配置,以将代理设置为首先运行 initConfig:
module.exports = function(grunt) {
[...]
grunt.initConfig({
connect: {
proxies: [{
host: grunt.config.get('proxyHost')
这篇文章(在 initConfig(( 中访问 Grunt 配置数据(概述了这个问题,但我不确定如何在任务之外同步运行请求?
编辑2 [已解决]:
科里斯答案+这篇文章 以编程方式将参数传递给咕噜咜咕噜的任务? 为我解决了这个问题。
module.exports = function(grunt) {
[...]
grunt.initConfig({
connect: {
proxies: [{
host: '<%= proxyHost %>',
您可以轻松地异步运行任务并通过 grunt config 对象在任务之间共享数据,而不是同步运行任务。
1. 异步运行任务
要异步运行任务,您必须首先通过调用 Grunt 来通知 Grunt 该任务将是异步的this.async()
此异步调用返回一个"done 函数",您将使用该函数告诉 Grunt 任务是通过还是失败。可以通过传递处理程序 true 来完成任务,并通过向其传递错误或 false 来失败。
异步任务:
module.exports = function(grunt) {
grunt.registerTask('foo', 'description of foo', function() {
var done = this.async();
request('http://www...', function(err, resp, body) {
if ( err ) {
done(false); // fail task with `false` or Error objects
} else {
grunt.config.set('proxyHost', someValue);
done(true); // pass task
}
});
});
}
2. 在任务之间共享数据
该grunt.config.set()
位(在上面的代码中(可能是在任务之间共享值的最简单方法。由于所有任务共享相同的 grunt 配置,因此您只需在配置上设置一个属性,然后通过 grunt.config.get('property')
从以下任务中读取它
后续任务
module.exports = function(grunt) {
grunt.registerTask('bar', 'description of bar', function() {
// If proxy host is not defined, the task will die early.
grunt.config.requires('proxyHost');
var proxyHost = grunt.config.get('proxyHost');
// ...
});
}
grunt.config.requires
位将通知 grunt 任务具有配置依赖项。这在任务长时间未触及(非常常见(并且忘记复杂性的情况下很有用。如果您决定更改异步任务(重命名变量?dev_proxyHost,prod_proxyHost?(,以下任务将优雅地通知您找不到proxyHost
。
Verifying property proxyHost exists in config...ERROR
>> Unable to process task.
Warning: Required config property "proxyHost" missing. Use --force to continue.
3. 您的代码,异步
grunt.registerTask('setProxyHost', 'Pings the url shortener to get the latest test server', function() {
var done = this.async(),
loc,
proxyHost;
request('http://urlshortener/devserver', function(error, response, body) {
if (error) {
done(error); // error out early
}
loc = response.request.uri.href;
if (loc.slice(0, 7) === 'http://') {
proxyHost = loc.slice(7, loc.length - 1);
// set proxy host on grunt config, so that it's accessible from the other task
grunt.config.set('proxyHost', proxyHost);
done(true); // success
}
else {
done(new Error("Unexpected HTTPS Error: The devserver urlshortener unexpectedly returned an HTTPS link! ("+loc+")"));
}
});
});
然后,从代理任务中,您可以通过以下方式检索此代理主机值
var proxyHost = grunt.config.get('proxyHost');
- Gulp Git首先运行提交任务,然后运行推送任务
- 如何在jshint任务运行且一切正常时记录消息
- 如何按顺序运行gullow任务
- 我一直收到的控制台警告是什么?推迟长时间运行的计时器任务以提高滚动的流畅性
- 如何在Grunt.js中观察多个文件,但只在更改的文件上运行任务
- 在节点.js的多个线程中运行任务
- 如何按特定顺序运行 Gulp 任务
- 如何在咕噜声插件中运行其他已注册的咕噜声任务
- Angularjs推迟了日志运行计时器任务
- 在Meteor.js中运行后台任务
- 如何使用参数按顺序运行三个任务
- 如何从Node.js运行gullow任务-gullow.start()的任何更好的替代方案
- 从gullow任务运行一个npm脚本
- 一个咕哝的任务能运行其他咕哝的任务吗
- 使用nodejs对两个任务运行异步
- 不能让Grunt ' watch '任务运行
- Gulp任务运行缓慢
- Gulp插件包括/排除行JS代码取决于任务运行
- 如何用Jasmine在Angular应用程序中模拟d3.json()调用,用Karma任务运行器进行测试
- Gradle任务运行Nashorn JavaScript