Skip to content

Instantly share code, notes, and snippets.

@quidmonkey
Last active August 29, 2015 14:16
Show Gist options
  • Save quidmonkey/460809dc96c883fa7e5d to your computer and use it in GitHub Desktop.
Save quidmonkey/460809dc96c883fa7e5d to your computer and use it in GitHub Desktop.
Get All Bower Components in Node
'use strict';
var fs = require('fs');
var globule = require('globule');
var path = require('path');
var bowerComponents = globule
.find('bower_components/**/bower.json')
.map(function (fileName) {
var bowerJson = fs.readFileSync(fileName);
var componentFile = JSON.parse(bowerJson).main;
var componentPath = fileName.replace('bower.json', '');
// main may be an array of files
if (componentFile instanceof Array) {
return componentFile.map(function (component) {
return path.join(componentPath, component);
});
} else {
return path.join(componentPath, componentFile);
}
})
// reverse sorting
// so that jquery loads before jquery-ui
// and angular prior to its plugins
// TODO fix with more permanent solution later
.sort(function (a, b) {
return b > a;
});
// flatten array
module.exports = [].concat.apply([], bowerComponents);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment