Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically install with bower?

I'm writing a grunt task and I want to install a dependency programmatically. However, I can't seem to figure out how to use their API.

This works just fine, but parsing the response is brittle because it uses the CLI:

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

This does not work:

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();
    });

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?

like image 270
Nick Heiner Avatar asked Feb 18 '23 14:02

Nick Heiner


1 Answers

The line() function expects the whole argv, so should be:

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

However, you should rather just pass paths and options to the install() method directly:

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

like image 108
Sindre Sorhus Avatar answered Feb 20 '23 02:02

Sindre Sorhus