1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict'
|
10 |
|
11 | const aglob = require('aglob')
|
12 | const path = require('path')
|
13 |
|
14 |
|
15 | async function resolve (patterns) {
|
16 | const result = []
|
17 | for (const pattern of [].concat(patterns || [])) {
|
18 | switch (typeof pattern) {
|
19 | case 'object':
|
20 | let fromObject = await resolve.fromObject(pattern)
|
21 | result.push(...fromObject)
|
22 | break
|
23 | case 'function':
|
24 | let fromFunction = await resolve.fromFunction(pattern)
|
25 | result.push(...fromFunction)
|
26 | break
|
27 | default:
|
28 | let fromString = await resolve.fromString(pattern)
|
29 | result.push(...fromString)
|
30 | break
|
31 | }
|
32 | }
|
33 | return result.reduce((a, b) => [].concat(a, b), [])
|
34 | }
|
35 |
|
36 | Object.assign(resolve, {
|
37 | async fromObject (src) {
|
38 | const bud = src
|
39 | bud.cwd = bud.cwd || _cwd()
|
40 | return [].concat(bud)
|
41 | },
|
42 | async fromFunction (src) {
|
43 | let bud = awaitsrc()
|
44 | return await
|
45 | resolve(bud)
|
46 | },
|
47 | async fromString (src) {
|
48 | const filenames = await
|
49 | aglob(src)
|
50 | const notFound = (src.length > 0) && (filenames.length === 0)
|
51 | if (notFound) {
|
52 | console.warn('File not found with pattern:', [].concat(src))
|
53 | }
|
54 | return filenames
|
55 | }
|
56 | })
|
57 |
|
58 | function _cwd () {
|
59 | const parent = module.parent
|
60 | return (parent && path.dirname(parent.filename)) || process.cwd()
|
61 | }
|
62 |
|
63 | module.exports = resolve
|