UNPKG

1.14 kBJavaScriptView Raw
1/**
2 * Expand multiple glob patterns.
3 * @function expandglob
4 */
5
6'use strict'
7
8const argx = require('argx')
9const glob = require('glob')
10const async = require('async')
11const co = require('co')
12const pkg = require('./package.json')
13const arrayreduce = require('arrayreduce')
14
15/** @lends expandglob */
16function 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
38expandglob.sync = function (patterns, options) {
39 return [].concat(patterns || []).map((pattern) =>
40 glob.sync(pattern, options)
41 ).reduce(arrayreduce.arrayConcat(), [])
42}
43
44module.exports = expandglob