1 | 'use strict';
|
2 |
|
3 | const q = require('q');
|
4 | const qfs = require('q-io/fs');
|
5 | const _ = require('lodash');
|
6 | const glob = require('glob');
|
7 | const defaults = require('./defaults');
|
8 | const utils = require('./utils');
|
9 | const resolvePath = require('path').resolve;
|
10 |
|
11 | const isMask = (pattern) => glob.hasMagic(pattern);
|
12 |
|
13 | const getFilesByMask = (pattern, options) => q.nfcall(glob, pattern, options);
|
14 |
|
15 | const listFiles = (path) => {
|
16 | return qfs.listTree(path)
|
17 | .then((paths) => utils.asyncFilter(paths, utils.isFile));
|
18 | };
|
19 |
|
20 | const expandPath = (path, options) => {
|
21 | path = options.root ? resolvePath(options.root, path) : path;
|
22 |
|
23 | return utils.isFile(path)
|
24 | .then((isFile) => isFile ? [path] : listFiles(path))
|
25 | .then((paths) => paths.filter((path) => utils.matchesFormats(path, options.formats)));
|
26 | };
|
27 |
|
28 | const processPaths = (paths, cb) => {
|
29 | return _(paths)
|
30 | .map(cb)
|
31 | .thru(q.all)
|
32 | .value()
|
33 | .then(_.flatten)
|
34 | .then(_.uniq);
|
35 | };
|
36 |
|
37 | exports.expandPaths = (paths, expandOpts, globOpts) => {
|
38 | expandOpts = defaults(expandOpts);
|
39 |
|
40 | paths = [].concat(paths);
|
41 |
|
42 | return processPaths(paths, (path) => getFilesByMask(path, globOpts))
|
43 | .then((matchedPaths) => processPaths(matchedPaths, (path) => expandPath(path, expandOpts)));
|
44 | };
|
45 |
|
46 | exports.isMask = isMask;
|