2013-02-08 46 views
4

我正在寫一個咕task任務,我想以編程方式安裝依賴項。但是,我似乎無法弄清楚如何使用他們的API。以編程方式與bower一起安裝?

這只是正常,但解析響應是脆弱,因爲它使用CLI:

grunt.util.spawn({ 
    cmd: 'bower', 
    args: ['install', '--save', '[email protected]:foo/bar.git'] 
}, function(none, message) { 
    grunt.log.writeln(message); 
}); 

這不起作用:

bower.commands.install.line(['--save', '[email protected]:foo/bar.git']) 
    .on('end', function(data) { 
     grunt.log.writeln(data); 
     done(); 
    }) 
    .on('err', function(err) { 
     grunt.log.fail(err); 
     done(); 
    }); 

我收到以下錯誤:

$ grunt my-task 
Running "my-task:default_options" (my-task) task 
Fatal error: Could not find any dependencies 

什麼是正確的方法來做到這一點?

回答

8

line()功能預計全部argv,所以應該是:

bower.commands.install.line(['node', 'bower', '--save', '[email protected]:foo/bar.git']);

然而,你應該寧可只是傳遞路徑和選項的install()方法直接:

bower.commands.install(['[email protected]:foo/bar.git'], {save: true});