main-bower-files
Advanced tools
Comparing version 2.12.0 to 2.13.0
@@ -75,5 +75,4 @@ var path = require('path'), | ||
bowerJson = JSON.parse(stripJsonComments(readFile(this.opts.paths.bowerJson, 'utf8'))), | ||
isExludingGroup = (group && bowerJson.group && group.charAt(0) === "!" && bowerJson.group[group.slice(1)].length > 0), | ||
devDependencies = (isExludingGroup ? this.filterByGroup(bowerJson.devDependencies, bowerJson.group[group.slice(1)]) : bowerJson.devDependencies) || {}, | ||
dependencies = (isExludingGroup ? this.filterByGroup(bowerJson.dependencies, bowerJson.group[group.slice(1)]) : bowerJson.dependencies) || {}, | ||
devDependencies = bowerJson.devDependencies || {}, | ||
dependencies = bowerJson.dependencies || {}, | ||
main = bowerJson.main || {}; | ||
@@ -84,42 +83,93 @@ | ||
this.overrides = extend(bowerJson.overrides || {}, this.overrides); | ||
// add packages from group option, add all except in group, or add packages entirely from bower file | ||
if (group && bowerJson.group && !isExludingGroup) { | ||
if (!bowerJson.group[group]) { | ||
throw new Error('group "' + group + '" does not exists in bower.json'); | ||
} | ||
var deps = bowerJson.group[group]; | ||
this.checkGroupExists(group, bowerJson, function (missingGroup) { | ||
throw new Error('group "' + missingGroup + '" does not exists in bower.json'); | ||
}); | ||
if (deps instanceof Array) { | ||
for (var i in deps) { | ||
this.add(deps[i], path.join(this.opts.paths.bowerDirectory, path.sep, deps[i])); | ||
} | ||
if (includeDev !== 'exclusive') { | ||
this.addDependencies(dependencies, group, bowerJson); | ||
} | ||
if (includeDev !== false) { | ||
this.addDependencies(devDependencies, group, bowerJson); | ||
} | ||
if (includeSelf !== false) { | ||
this.addDependencies(main); | ||
} | ||
}, | ||
/** | ||
* Adds all dependencies from list filtered by group | ||
* | ||
*/ | ||
addDependencies: function (dependencies, group, bowerJson) { | ||
if (typeof dependencies !== "string") { | ||
var deps = (!!group) ? this.filterByGroup(dependencies, group, bowerJson) : dependencies; | ||
for (var name in deps) { | ||
this.add(name, path.join(this.opts.paths.bowerDirectory, path.sep, name)); | ||
} | ||
} else { | ||
if (includeDev !== 'exclusive') { | ||
for (name in dependencies) { | ||
this.add(name, path.join(this.opts.paths.bowerDirectory, path.sep, name)); | ||
} | ||
} | ||
this.add(dependencies, path.join(path.dirname(this.opts.paths.bowerJson))); | ||
} | ||
}, | ||
if (includeDev !== false) { | ||
for (name in devDependencies) { | ||
this.add(name, path.join(this.opts.paths.bowerDirectory, path.sep, name)); | ||
/** | ||
* Filters dependencies by group | ||
* | ||
* @return {Object} | ||
*/ | ||
filterByGroup: function (deps, group, bowerJson) { | ||
var filtered = {}; | ||
if (typeof group === "string") { | ||
var isExludingGroup = (group && bowerJson.group && group.charAt(0) === "!" && bowerJson.group[group.slice(1)].length > 0); | ||
for (var dep in deps) { | ||
if (isExludingGroup && bowerJson.group[group.slice(1)].indexOf(dep) === -1) { | ||
filtered[dep] = deps[dep]; | ||
} | ||
if (!isExludingGroup && bowerJson.group[group].indexOf(dep) >= 0) { | ||
filtered[dep] = deps[dep]; | ||
} | ||
} | ||
if (includeSelf !== false) { | ||
this.add(main, path.join(path.dirname(this.opts.paths.bowerJson))); | ||
return filtered; | ||
} | ||
if (typeof group === "object") { | ||
for (var i = 0; i < group.length; i++) { | ||
filtered = extend(filtered, this.filterByGroup(deps, group[i], bowerJson)); | ||
} | ||
} | ||
return filtered; | ||
}, | ||
filterByGroup: function (deps, group) { | ||
var filtered = {}; | ||
for (var dep in deps) { | ||
if (group.indexOf(dep) === -1) { | ||
filtered[dep] = deps[dep]; | ||
/** | ||
* Calls error method if group doesn't exist | ||
*/ | ||
checkGroupExists: function (group, bowerJson, error) { | ||
if(!group || !bowerJson.group) { | ||
return; | ||
} | ||
if (typeof group === "string") { | ||
var isExludingGroup = (group && bowerJson.group && group.charAt(0) === "!" && bowerJson.group[group.slice(1)].length > 0); | ||
if(!bowerJson.group[group] && !isExludingGroup) { | ||
error(group); | ||
return; | ||
} | ||
return bowerJson.group[group]; | ||
} | ||
return filtered; | ||
if (typeof group === "object") { | ||
for (var i = 0; i < group.length; i++) { | ||
this.checkGroupExists(group[i], bowerJson, error); | ||
} | ||
} | ||
}, | ||
@@ -126,0 +176,0 @@ |
{ | ||
"name": "main-bower-files", | ||
"version": "2.12.0", | ||
"version": "2.13.0", | ||
"description": "Get main files from your installed bower packages.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -267,3 +267,3 @@ main-bower-files | ||
Type: `String` Default: `null` | ||
Type: `String` or `Array` Default: `null` | ||
@@ -284,2 +284,3 @@ You can specify a group of dependencies you want to read from bower.json | ||
"home": [ "BOWER-PACKAGE-1" ], | ||
"contact": [ "BOWER-PACKAGE-4" ], | ||
"admin": [ "BOWER-PACKAGE-1", "BOWER-PACKAGE-2", "BOWER-PACKAGE-3" ] | ||
@@ -294,2 +295,8 @@ } | ||
You can select multiple groups with an array. | ||
```javascript | ||
mainBowerFiles({ paths: 'path/for/project', group: ['home', 'contact'] }); | ||
``` | ||
You can include all packages except for those listed in a group with the `!` operator. | ||
@@ -296,0 +303,0 @@ |
@@ -13,4 +13,5 @@ { | ||
"group1": [ "simple", "multi" ], | ||
"group2": [ "overwritten" ], | ||
"containDepsError": [ "simple", "nonExistingModule" ] | ||
} | ||
} |
@@ -313,2 +313,11 @@ var mainBowerFiles = require('../'), | ||
it('should select the expected files from group property in bower.json defined by array of groups', function(done) { | ||
expect([ | ||
'/fixtures/simple/simple.js', | ||
'/fixtures/multi/multi.js', | ||
'/fixtures/multi/multi.css', | ||
'/fixtures/overwritten/overwritten.js' | ||
]).fromConfig('/_bower_with_group.json', { group: ['group1', 'group2'] }).when(done); | ||
}); | ||
it('should select all files except those listed in the group property in bower.json', function(done) { | ||
@@ -315,0 +324,0 @@ expect([ |
44659
927
329