globby
Advanced tools
Comparing version 1.1.0 to 1.2.0
85
index.js
'use strict'; | ||
var union = require('array-union'); | ||
var assign = require('object-assign'); | ||
var async = require('async'); | ||
var glob = require('glob'); | ||
var Minimatch = require('minimatch').Minimatch; | ||
@@ -11,10 +11,5 @@ function arrayify(arr) { | ||
module.exports = function (patterns, opts, cb) { | ||
function sortPatterns(patterns) { | ||
patterns = arrayify(patterns); | ||
if (typeof opts === 'function') { | ||
cb = opts; | ||
opts = {}; | ||
} | ||
var positives = []; | ||
@@ -24,9 +19,37 @@ var negatives = []; | ||
patterns.forEach(function (pattern, index) { | ||
(pattern[0] === '!' ? negatives : positives).push({ | ||
var isNegative = pattern[0] === '!'; | ||
(isNegative ? negatives : positives).push({ | ||
index: index, | ||
pattern: pattern | ||
pattern: isNegative ? pattern.slice(1) : pattern | ||
}); | ||
}); | ||
if (positives.length === 0) { | ||
return { | ||
positives: positives, | ||
negatives: negatives | ||
}; | ||
} | ||
function setIgnore(opts, negatives, positiveIndex) { | ||
opts = assign({}, opts); | ||
var negativePatterns = negatives.filter(function (negative) { | ||
return negative.index > positiveIndex; | ||
}).map(function (negative) { | ||
return negative.pattern; | ||
}); | ||
opts.ignore = (opts.ignore || []).concat(negativePatterns); | ||
return opts; | ||
} | ||
module.exports = function (patterns, opts, cb) { | ||
var sortedPatterns = sortPatterns(patterns); | ||
if (typeof opts === 'function') { | ||
cb = opts; | ||
opts = {}; | ||
} | ||
if (sortedPatterns.positives.length === 0) { | ||
cb(null, []); | ||
@@ -36,9 +59,5 @@ return; | ||
negatives.forEach(function (negative) { | ||
negative.matcher = new Minimatch(negative.pattern, opts); | ||
}); | ||
async.parallel(positives.map(function (positive) { | ||
async.parallel(sortedPatterns.positives.map(function (positive) { | ||
return function (cb2) { | ||
glob(positive.pattern, opts, function (err, paths) { | ||
glob(positive.pattern, setIgnore(opts, sortedPatterns.negatives, positive.index), function (err, paths) { | ||
if (err) { | ||
@@ -49,18 +68,3 @@ cb2(err); | ||
var negativeMatchers = negatives.filter(function (negative) { | ||
return negative.index > positive.index; | ||
}).map(function (negative) { | ||
return negative.matcher; | ||
}); | ||
if (negativeMatchers.length === 0) { | ||
cb2(null, paths); | ||
return; | ||
} | ||
cb2(null, paths.filter(function (path) { | ||
return negativeMatchers.every(function (matcher) { | ||
return matcher.match(path); | ||
}); | ||
})); | ||
cb2(null, paths); | ||
}); | ||
@@ -79,20 +83,11 @@ }; | ||
module.exports.sync = function (patterns, opts) { | ||
patterns = arrayify(patterns); | ||
var sortedPatterns = sortPatterns(patterns); | ||
if (patterns.length === 0) { | ||
if (sortedPatterns.positives.length === 0) { | ||
return []; | ||
} | ||
opts = opts || {}; | ||
return patterns.reduce(function (ret, pattern) { | ||
if (pattern[0] === '!') { | ||
var matcher = new Minimatch(pattern, opts); | ||
return ret.filter(function (path) { | ||
return matcher.match(path); | ||
}); | ||
} | ||
return union(ret, glob.sync(pattern, opts)); | ||
return sortedPatterns.positives.reduce(function (ret, positive) { | ||
return union(ret, glob.sync(positive.pattern, setIgnore(opts, sortedPatterns.negatives, positive.index))); | ||
}, []); | ||
}; |
{ | ||
"name": "globby", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Extends `glob` with support for multiple patterns", | ||
@@ -10,3 +10,3 @@ "license": "MIT", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "http://sindresorhus.com" | ||
"url": "sindresorhus.com" | ||
}, | ||
@@ -57,4 +57,4 @@ "engines": { | ||
"async": "^0.9.0", | ||
"glob": "^4.0.2", | ||
"minimatch": "^2.0.1" | ||
"glob": "^4.4.0", | ||
"object-assign": "^2.0.0" | ||
}, | ||
@@ -61,0 +61,0 @@ "devDependencies": { |
@@ -8,3 +8,3 @@ # globby [![Build Status](https://travis-ci.org/sindresorhus/globby.svg?branch=master)](https://travis-ci.org/sindresorhus/globby) | ||
```sh | ||
``` | ||
$ npm install --save globby | ||
@@ -11,0 +11,0 @@ ``` |
5856
71
+ Addedobject-assign@^2.0.0
+ Addedobject-assign@2.1.1(transitive)
- Removedminimatch@^2.0.1
Updatedglob@^4.4.0