1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict'
|
7 |
|
8 | const argx = require('argx')
|
9 | const glob = require('glob')
|
10 | const async = require('async')
|
11 | const co = require('co')
|
12 | const pkg = require('./package.json')
|
13 | const arrayreduce = require('arrayreduce')
|
14 |
|
15 |
|
16 | function expandglob (patterns, options) {
|
17 | let args = argx(arguments)
|
18 | let callback = args.pop('function')
|
19 | options = args.pop('object') || {}
|
20 |
|
21 | patterns = [].concat(patterns || [])
|
22 |
|
23 | return co(function * () {
|
24 | return yield new Promise((resolve, reject) =>
|
25 | async.concat(patterns, (pattern, callback) => {
|
26 | glob(pattern, options, callback)
|
27 | }, (err, filenames) => {
|
28 | if (callback) {
|
29 | console.warn(`[${pkg.name}] Callback is now deprecated. Use promise interface instead.`)
|
30 | callback(err, filenames)
|
31 | }
|
32 | err ? reject(err) : resolve(filenames)
|
33 | })
|
34 | )
|
35 | })
|
36 | }
|
37 |
|
38 | expandglob.sync = function (patterns, options) {
|
39 | return [].concat(patterns || []).map((pattern) =>
|
40 | glob.sync(pattern, options)
|
41 | ).reduce(arrayreduce.arrayConcat(), [])
|
42 | }
|
43 |
|
44 | module.exports = expandglob
|