Comparing version 0.1.4 to 0.2.0
28
index.js
@@ -9,2 +9,3 @@ 'use strict'; | ||
var async = require('async'); | ||
var node_path = require('path'); | ||
@@ -40,3 +41,9 @@ // Process specified wildcard glob patterns or filenames against a | ||
function isGlob (pattern) { | ||
return ~ pattern.indexOf('*'); | ||
} | ||
// @param {options} | ||
// - globOnly: {Boolean} only deal with glob stars | ||
function expand(patterns, options, callback) { | ||
@@ -52,5 +59,12 @@ patterns = Array.isArray(patterns) ? patterns : [patterns]; | ||
async.parallel( | ||
patterns.map(function(pattern) { | ||
patterns | ||
.filter(Boolean) | ||
.map(function(pattern) { | ||
return function(done) { | ||
glob(pattern, options, done); | ||
if (options.globOnly && !isGlob(pattern)) { | ||
pattern = node_path.join('.', pattern); | ||
done(null, [pattern]); | ||
} else { | ||
glob(pattern, options, done); | ||
} | ||
}; | ||
@@ -89,6 +103,10 @@ }), | ||
processPatterns(patterns, function(pattern) { | ||
// Find all matching files for this pattern. | ||
return glob.sync(pattern, options); | ||
if (options.globOnly && !isGlob(pattern)) { | ||
pattern = node_path.join('.', pattern); | ||
return [pattern]; | ||
} else { | ||
// Find all matching files for this pattern. | ||
return glob.sync(pattern, options); | ||
} | ||
}); | ||
} |
{ | ||
"name": "fs-expand", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "An extended fs glob", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -18,3 +18,31 @@ # fs-expand [](http://badge.fury.io/js/fs-expand) [](https://travis-ci.org/kaelzhang/node-fs-expand) [](https://gemnasium.com/kaelzhang/node-fs-expand) | ||
### Additioanl `options` | ||
- globOnly `Boolean=false` only process glob patterns, if a file does not contain globstars, `fs-expand` will not check the existence of the file. | ||
``` | ||
<cwd>/ | ||
|-- a.js | ||
|-- b.js | ||
``` | ||
```js | ||
expand([ | ||
'*.js', | ||
'abc.md' | ||
], { | ||
cwd: cwd, | ||
globOnly: true | ||
}, function(err, files){ | ||
files; | ||
// -> | ||
// [ | ||
// 'a.js', | ||
// 'b.js', | ||
// 'abc.md' // actually, abc.md doesn't exist. | ||
// ] | ||
}); | ||
``` | ||
### Example | ||
@@ -21,0 +49,0 @@ |
@@ -53,2 +53,16 @@ 'use strict'; | ||
}); | ||
it("options.globOnly", function(done){ | ||
expand([ | ||
'*.js', | ||
'abc.md' | ||
], { | ||
cwd: fixtures, | ||
globOnly: true | ||
}, function (err, files) { | ||
expect(err).to.equal(null); | ||
expect(files.pop()).to.equal('abc.md'); | ||
done(); | ||
}); | ||
}); | ||
}); |
8379
146
77