1 | 'use strict'
|
2 |
|
3 | const glob = require('./lib/globPromise')
|
4 | const func = require('./func')
|
5 | const filesToStream = require('./lib/filesToStream')
|
6 | const streamToString = require('./lib/streamToString')
|
7 |
|
8 | const _defaults = {
|
9 | glob: {},
|
10 | stream: false
|
11 | }
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | module.exports = function (patterns, options, callback) {
|
25 | if (func.typeOf(options) === 'function') {
|
26 | callback = options
|
27 | options = {}
|
28 | }
|
29 |
|
30 | let settings = func.defaults(_defaults, options)
|
31 | let promise = glob(patterns, settings.glob)
|
32 | .then(filesToStream)
|
33 | .then((stream) => {
|
34 | if (settings.stream) {
|
35 | return stream
|
36 | }
|
37 |
|
38 | return streamToString(stream)
|
39 | })
|
40 | .then((result) => {
|
41 | if (callback) {
|
42 | callback(null, result)
|
43 | } else {
|
44 | return result
|
45 | }
|
46 | })
|
47 | .catch((err) => {
|
48 | if (callback) {
|
49 | callback(err)
|
50 | } else {
|
51 | throw err
|
52 | }
|
53 | })
|
54 |
|
55 | if (!callback) {
|
56 | return promise
|
57 | }
|
58 | }
|