Skip to content

Instantly share code, notes, and snippets.

@newtriks
Created Jul 3, 2014
Embed
What would you like to do?
Gulp task to run bower install/update and remove dependencies.
var gulp = require('gulp');
var bower = require('bower');
var del = require('del');
var _ = require('lodash');
var deleteDeps = function (deps, cb) {
var path = bower.config.cwd + "/" + bower.config.directory;
_.each(deps, function (dep) {
del(path + "/" + dep, function (e) {});
});
cb();
};
var bowerTask = function (fn, cb) {
var deps = _.chain(require(bower.config.cwd + '/bower.json').dependencies).keys().value();
var pkgs = [];
bower.commands[fn](deps)
.on('log', function (o) {
if (o.id !== 'install') {
return;
}
pkgs.push(o.data.endpoint.name);
})
.on('end', function (installed) {
deleteDeps(_.difference(pkgs, deps), cb);
});
};
gulp.task('bowerInstall', function (cb) {
bowerTask('install', cb);
});
gulp.task('bowerUpdate', function (cb) {
bowerTask('update', cb);
});
@newtriks
Copy link
Author

newtriks commented Jul 3, 2014

I use this task when using Restangular (not Browserify compatible) in a Browserify based project. Restangular dependencies include Angular and Lodash, I already have these via npm, so do not need the duplication.

This takes no prisoners, all dependencies of the installed packages are removed!

If I have missed a Bower configuration option that disables installing dependencies I would be keen to hear!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment