首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以在一个任务中调用两个bower.commands函数?

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

Stack Overflow用户
提问于 2013-11-18 22:40:53
回答 1查看 285关注 0票数 1

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

代码语言:javascript
复制
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');
        });
    });
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-19 19:00:48

所以,除了睡眠不足之外,我还发现了问题所在。我调用了bower_done();在bower.commands.info中,当它只在安装中完成任务线程时。换言之:

代码语言:javascript
复制
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');
        });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20059520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档