UNPKG

1.45 kBJavaScriptView Raw
1'use strict'
2
3const glob = require('./lib/globPromise')
4const func = require('./func')
5const filesToStream = require('./lib/filesToStream')
6const streamToString = require('./lib/streamToString')
7
8const _defaults = {
9 glob: {},
10 stream: false
11}
12
13/**
14 * Find files using glob pattern(s) get a string or stream of
15 * the combined files' content.
16 * @param {String|String[]} patterns - One or more glob patterns.
17 * @param {Object} [options] - Options object. Optional.
18 * @param {Boolean} [options.stream=false] - Get results as a stream. Optional.
19 * @param {Object} [options.glob={}] - Options to send to glob.
20 * @param {Function} [callback]
21 * @returns {Promise} Returns a promise if no callback is provided.
22 * @see {@link https://www.npmjs.com/package/glob}
23 */
24module.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}