|
1 | 1 | var glob = require("glob"); |
2 | | -var async = require("async"); |
3 | | -var _ = require("lodash"); |
| 2 | + |
| 3 | +exports._glob = glob; |
| 4 | + |
| 5 | +function uniq(xs) { |
| 6 | + return xs.reduce((unique, x) => { |
| 7 | + return unique.indexOf(x) < 0 ? unique.concat(x) : unique; |
| 8 | + }, []); |
| 9 | +} |
| 10 | + |
| 11 | +function flatten(xs) { |
| 12 | + return xs.reduce((flattened, x) => { |
| 13 | + return flattened.concat(Array.isArray(x) ? flatten(x) : x); |
| 14 | + }, []); |
| 15 | +} |
4 | 16 |
|
5 | 17 | function array(arr) { |
6 | 18 | return Array.isArray(arr) ? arr : [arr]; |
7 | 19 | } |
8 | 20 |
|
9 | 21 | function resolveGlobs(patterns, options) { |
10 | 22 | options = options || {}; |
11 | | - return array(patterns).reduce(function (fns, pattern) { |
12 | | - fns.push(function (done) { |
13 | | - glob(pattern, options, function (err, matches) { |
14 | | - if (!err && options.strict && matches.length === 0) { |
15 | | - done(new Error("'" + pattern + "' matched no files")); |
16 | | - } else { |
17 | | - done(err, matches); |
18 | | - } |
| 23 | + return Promise.all( |
| 24 | + array(patterns).map(pattern => new Promise((resolve, reject) => { |
| 25 | + exports._glob(pattern, options, (err, matches) => { |
| 26 | + if (!err && options.strict && matches.length === 0) { |
| 27 | + reject(new Error("'" + pattern + "' matched no files")); |
| 28 | + } else if (err) { |
| 29 | + reject(err); |
| 30 | + } else { |
| 31 | + resolve(matches); |
| 32 | + } |
19 | 33 | }); |
20 | | - }); |
21 | | - return fns; |
22 | | - }, []); |
| 34 | + })) |
| 35 | + ); |
23 | 36 | } |
24 | 37 |
|
25 | | -function processSingle(callback) { |
26 | | - return function (err, matches) { |
27 | | - callback(err, _.uniq(_.flatten(_.toArray(matches)))); |
28 | | - }; |
29 | | -} |
| 38 | +exports.glob = function (patterns, options, cb) { |
| 39 | + if (typeof options === "function") { |
| 40 | + cb = options; |
| 41 | + options = null; |
| 42 | + } |
30 | 43 |
|
31 | | -module.exports = { |
32 | | - glob: function (patterns, options, cb) { |
33 | | - if (typeof options === "function") { |
34 | | - cb = options; |
35 | | - options = null; |
36 | | - } |
37 | | - async.parallel(resolveGlobs(patterns, options), processSingle(cb)); |
38 | | - } |
| 44 | + resolveGlobs(patterns, options) |
| 45 | + .then(matches => cb(null, uniq(flatten(array(matches))))) |
| 46 | + .catch(err => cb(err)); |
39 | 47 | }; |
0 commit comments