Skip to content

Instantly share code, notes, and snippets.

@newtriks
Created July 3, 2014 11:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newtriks/96c306e9cdde5c26f2b1 to your computer and use it in GitHub Desktop.
Save newtriks/96c306e9cdde5c26f2b1 to your computer and use it in GitHub Desktop.
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