Gruntjs 是否可以在一个grunt任务中调用两个bower.commands函数?

Gruntjs 是否可以在一个grunt任务中调用两个bower.commands函数?,gruntjs,bower,Gruntjs,Bower,我最近开始学习如何使用GruntJS和Bower。我的任务是在grunt任务中使用bower的编程api。我试图实现的是调用bower.commands.info,将所需项目的bower.json中的文件路径与我希望它进入的本地文件路径进行比较。然后,它应该将所述项目安装到本地路径中。到目前为止,它只调用bower.commands.info,但忘记了bower.commands.install。我甚至改变了他们被呼叫的顺序,看看这是否会影响任何事情。它只显示信息,但没有安装。希望我的代码能解释

我最近开始学习如何使用GruntJS和Bower。我的任务是在grunt任务中使用bower的编程api。我试图实现的是调用bower.commands.info,将所需项目的bower.json中的文件路径与我希望它进入的本地文件路径进行比较。然后,它应该将所述项目安装到本地路径中。到目前为止,它只调用bower.commands.info,但忘记了bower.commands.install。我甚至改变了他们被呼叫的顺序,看看这是否会影响任何事情。它只显示信息,但没有安装。希望我的代码能解释更多:

module.exports = function(grunt){

    grunt.initConfig({
        //this is where the grunt tasks go

        //this is where the package info is read in
        pkg: grunt.file.readJSON('package.json'), //use comma if adding npm tasks

        'bower-install': {
            target: {
                //point to the html file that is to be updated
                html: 'index.html',

                //Optional:
                //ignorePath: 'wxProj/',

                //customize how stylesheets are included
                cssPattern: '<link rel="stylesheet" href="{{filePath}}"/>',

                //customize how scripts are included
                jsPattern: '<script type="text/javascript" src="{{filePath}}"> </script>'
            }
        }
    });

    //feel free to load any npm tasks here
    grunt.loadNpmTasks('grunt-bower-install');

    //this function uses bower to pull some files from my github
    grunt.registerTask('extract', function(name){
        var bower = require('bower'),
            bower_done = this.async(),
            wxdesk_contents = '{\n' +
                '"directory" : "wxdesk/bower_components"\n' +
            '}',
            vendor_contents = '{\n' +
            '"directory" : "vendor/bower_components"\n'+
            '}';

        bower.commands.info(name, 'dest')
        .on('error', function(){
            bower_done(false);
        })
        .on('end', function(dest){
            bower_done();
            grunt.log.writeln(dest);
            if(dest == 'wxdesk'){
                grunt.log.writeln('written to wxdesk!');
                //change .bowerrc's "directory" property
                grunt.file.write('.bowerrc', wxdesk_contents);
            }
            else
            {
                grunt.log.writeln('written to vendor!');
                grunt.file.write('.bowerrc', vendor_contents);
            }
        });

        bower.commands.install([name], {save: true})
        .on('log', function(result){
            grunt.log.writeln(['bower', result.id.cyan, result.message].join(' '));
        })
        .on('error', function(){
            bower_done(false);
        })
        .on('end', function(results){
            bower_done();
            //run grunt bower-install
            grunt.task.run('bower-install');
        });
    });
}
module.exports=函数(grunt){
grunt.initConfig({
//这就是grunt任务的方向
//这是读取包信息的地方
pkg:grunt.file.readJSON('package.json'),//如果添加npm任务,请使用逗号
“bower安装”:{
目标:{
//指向要更新的html文件
html:'index.html',
//可选:
//ignorePath:'wxProj/',
//自定义样式表的包含方式
cssPattern:“”,
//自定义脚本的包含方式
jsPattern:“”
}
}
});
//请随意在此处加载任何npm任务
grunt.loadNpmTasks('grunt-bower-install');
//此函数使用bower从我的github中提取一些文件
grunt.registerTask('extract',函数(名称){
var bower=需要(“bower”),
bower_done=this.async(),
wxdesk_contents='{\n'+
““目录”:“wxdesk/bower\u组件”\n”+
'}',
供应商内容=“{\n”+
““目录”:“供应商/bower\u组件”\n”+
'}';
bower.commands.info(名称“dest”)
.on('error',function()){
bower_done(假);
})
.on('end',函数(dest){
鲍尔_done();
grunt.log.writeln(dest);
如果(dest=='wxdesk'){
grunt.log.writeln('writedtowxdesk!');
//change.bowerrc的“目录”属性
grunt.file.write('.bowerrc',wxdesk_contents);
}
其他的
{
grunt.log.writeln('write to vendor!');
grunt.file.write('.bowerrc',供应商目录);
}
});
install([name],{save:true})
.on('log',函数(结果){
grunt.log.writeln(['bower',result.id.cyan,result.message].join(“”));
})
.on('error',function()){
bower_done(假);
})
.on('end',函数(结果){
鲍尔_done();
//运行grunt bower安装
grunt.task.run('bower-install');
});
});
}

因此,除了睡眠不足之外,我发现了我的问题所在。我打电话给鲍尔_done();在bower.commands.info中,当它只应在安装中完成任务线程时。 换言之:

bower.commands.info(name, 'dest')
        .on('error', function(){
            bower_done(false);
        })
        .on('end', function(dest){
            ////////////////////////////////////////////////
            //bower_done(); <= This guy should not be here!
            ////////////////////////////////////////////////
            grunt.log.writeln(dest);
            if(dest == 'wxdesk'){
                grunt.log.writeln('written to wxdesk!');
                //change .bowerrc's "directory" property
                grunt.file.write('.bowerrc', wxdesk_contents);
            }
            else
            {
                grunt.log.writeln('written to vendor!');
                grunt.file.write('.bowerrc', vendor_contents);
            }
        });

        bower.commands.install([name], {save: true})
        .on('log', function(result){
            grunt.log.writeln(['bower', result.id.cyan, result.message].join(' '));
        })
        .on('error', function(){
            bower_done(false);
        })
        .on('end', function(results){
            ///////////////////////////////
            bower_done(); //<= should be here on the final task to end the async
            ///////////////////////////////
            //run grunt bower-install
            grunt.task.run('bower-install');
        });
bower.commands.info(名称“dest”)
.on('error',function()){
bower_done(假);
})
.on('end',函数(dest){
////////////////////////////////////////////////
//鲍尔_done();