Installing Bower Dependencies from Grunt

If you use both bower and grunt in your node.js project and you don't like to perform bower install manually every time while you already have a grunt default, here's a simple task you can add to your Gruntfile.js using bower's programmatic api:

grunt.registerTask('bower', 'Install bower dependencies', function() {
    var bower = require('bower'),
        done = this.async();
    bower.commands.install().on('log', function(result) {
        grunt.log.writeln('bower ' + result.id + ' ' + result.message);
    }).on('error', function() {
        done(false);
    }).on('end', function(results) {
        done();
    });
});

Note that it has to be asynchronous. Now you can add it into your default task:

grunt.registerTask('default', ['bower', 'concat', 'uglify']);

And make sure you have bower in your npm dependencies:

npm install bower --save

Now you don't even need to install bower globally and you can run everything you need with one grunt command.

comments powered by Disqus
Made with in SPb