I have a gruntfile setup so that I can develop my local angularjs frontend while forwarding all api requests to a java middle tier hosted seperately on the network.
This works great except the location of the server changes every few days and I have to continuously update the gruntfile with the latest server location.
The latest server location can be found by a URL shortening service that forwards to the correct location, so I can fetch it with this grunt task/node.js code:
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);
}
}
});
});
Of course this is asynchonous, and when I run it, grunt has already setup the proxy by the time the request completes.
How can I run this nodejs request synchronously or block grunt until it completes? This is just for development so hacky solutions welcomed.
Thanks
EDIT:
Great answer Cory, that has mostly solved the problem as grunt now waits for the task to complete before continuing. One last issue is that I can't access that config from the initConfig for setting the proxy as initConfig runs first:
module.exports = function(grunt) {
[...]
grunt.initConfig({
connect: {
proxies: [{
host: grunt.config.get('proxyHost')
This post (Access Grunt config data within initConfig()) outlines the issue but I'm not sure how I could run the request synchronously outside of a task?
EDIT2 [solved]:
Corys answer + this post Programmatically pass arguments to grunt task? solved the issue for me.
module.exports = function(grunt) {
[...]
grunt.initConfig({
connect: {
proxies: [{
host: '<%= proxyHost %>',