[英]Programmatically install with bower?
I'm writing a grunt task and I want to install a dependency programmatically. 我正在编写一个grunt任务,我想以编程方式安装依赖项。 However, I can't seem to figure out how to use their API.
但是,我似乎无法弄清楚如何使用他们的API。
This works just fine, but parsing the response is brittle because it uses the CLI: 这很好用,但解析响应很脆弱,因为它使用CLI:
grunt.util.spawn({
cmd: 'bower',
args: ['install', '--save', 'git@github.com:foo/bar.git']
}, function(none, message) {
grunt.log.writeln(message);
});
This does not work: 这不起作用:
bower.commands.install.line(['--save', 'git@github.com:foo/bar.git'])
.on('end', function(data) {
grunt.log.writeln(data);
done();
})
.on('err', function(err) {
grunt.log.fail(err);
done();
});
I get the following error: 我收到以下错误:
$ grunt my-task
Running "my-task:default_options" (my-task) task
Fatal error: Could not find any dependencies
What is the right way to do this? 这样做的正确方法是什么?
The line()
function expects the whole argv, so should be: line()
函数需要整个argv,所以应该是:
bower.commands.install.line(['node', 'bower', '--save', 'git@github.com:foo/bar.git']);
However, you should rather just pass paths and options to the install()
method directly: 但是,您应该直接将路径和选项直接传递给
install()
方法:
bower.commands.install(['git@github.com:foo/bar.git'], {save: true});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.