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

Run grunt task asynchronously before another task

本文关键字:任务 运行 grunt 异步 另一个      更新时间:2023-09-26

我有一个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');