UNPKG

1.42 kBJavaScriptView Raw
1"use strict";
2
3const glob = require('./lib/globPromise');
4
5const func = require('./func');
6
7const filesToStream = require('./lib/filesToStream');
8
9const streamToString = require('./lib/streamToString');
10
11const _defaults = {
12 glob: {},
13 stream: false
14};
15/**
16 * Find files using glob pattern(s) get a string or stream of
17 * the combined files' content.
18 * @param {String|String[]} patterns - One or more glob patterns.
19 * @param {Object} [options] - Options object. Optional.
20 * @param {Boolean} [options.stream=false] - Get results as a stream. Optional.
21 * @param {Object} [options.glob={}] - Options to send to glob.
22 * @param {Function} [callback]
23 * @returns {Promise} Returns a promise if no callback is provided.
24 * @see {@link https://www.npmjs.com/package/glob}
25 */
26
27module.exports = function (patterns, options, callback) {
28 if (func.typeOf(options) === 'function') {
29 callback = options;
30 options = {};
31 }
32
33 const settings = func.defaults(_defaults, options);
34 const promise = glob(patterns, settings.glob).then(filesToStream).then(stream => {
35 if (settings.stream) {
36 return stream;
37 }
38
39 return streamToString(stream);
40 }).then(result => {
41 if (callback) {
42 callback(null, result);
43 } else {
44 return result;
45 }
46 }).catch(err => {
47 if (callback) {
48 callback(err);
49 } else {
50 throw err;
51 }
52 });
53
54 if (!callback) {
55 return promise;
56 }
57};
\No newline at end of file