在另一个任务之前异步运行grunt任务

Aid*_*dan 9 javascript node.js gruntjs

我有一个gruntfile设置,以便我可以开发我的本地angularjs前端,同时将所有api请求转发到在网络上单独托管的java中间层.

这很好用,除了服务器的位置每隔几天更改一次,我必须不断更新gruntfile和最新的服务器位置.

最新的服务器位置可以通过URL缩短服务找到,该服务转发到正确的位置,因此我可以使用这个grunt task/node.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);
            }
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

当然这是异步的,当我运行它时,grunt已经在请求完成时设置了代理.

如何同步运行此nodejs请求或阻止grunt直到它完成?这只是为了开发,所以欢迎hacky解决方案.

谢谢

编辑:

伟大的答案Cory,这已经解决了这个问题,因为grunt现在等待任务完成才能继续.最后一个问题是我无法从initConfig访问该配置来设置代理,因为initConfig首先运行:

module.exports = function(grunt) {
[...]
    grunt.initConfig({
        connect: {
            proxies: [{
                host: grunt.config.get('proxyHost')
Run Code Online (Sandbox Code Playgroud)

这篇文章(initConfig()中的Access Grunt配置数据)概述了这个问题,但我不确定如何在任务之外同步运行请求?

EDIT2 [已解决]:

Corys回答+这篇文章以编程方式将参数传递给grunt任务?为我解决了这个问题.

module.exports = function(grunt) {
[...]
    grunt.initConfig({
        connect: {
           proxies: [{
                host: '<%= proxyHost %>',
Run Code Online (Sandbox Code Playgroud)

Cor*_*son 7

您可以通过grunt配置对象轻松地异步运行任务并在任务之间共享数据,而不是同步运行任务.


1.异步运行任务

要异步运行任务,必须首先通过调用通知Grunt任务将是异步的.this.async()异步调用返回一个"完成函数",您将用它来告诉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
            }
        });
    });
}
Run Code Online (Sandbox Code Playgroud)


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');
        // ...
    });
}
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)


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+")"));
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

然后,从您的代理任务,您可以通过以下方式检索此proxyHost值

var proxyHost = grunt.config.get('proxyHost');
Run Code Online (Sandbox Code Playgroud)