1
|
'use strict';
|
2
|
var Promise = require('pinkie-promise');
|
3
|
var arrayUnion = require('array-union');
|
4
|
var objectAssign = require('object-assign');
|
5
|
var glob = require('glob');
|
6
|
var pify = require('pify');
|
7
|
|
8
|
var globP = pify(glob, Promise).bind(glob);
|
9
|
|
10
|
function isNegative(pattern) {
|
11
|
return pattern[0] === '!';
|
12
|
}
|
13
|
|
14
|
function isString(value) {
|
15
|
return typeof value === 'string';
|
16
|
}
|
17
|
|
18
|
function assertPatternsInput(patterns) {
|
19
|
if (!patterns.every(isString)) {
|
20
|
throw new TypeError('patterns must be a string or an array of strings');
|
21
|
}
|
22
|
}
|
23
|
|
24
|
function generateGlobTasks(patterns, opts) {
|
25
|
patterns = [].concat(patterns);
|
26
|
assertPatternsInput(patterns);
|
27
|
|
28
|
var globTasks = [];
|
29
|
|
30
|
opts = objectAssign({
|
31
|
cache: Object.create(null),
|
32
|
statCache: Object.create(null),
|
33
|
realpathCache: Object.create(null),
|
34
|
symlinks: Object.create(null),
|
35
|
ignore: []
|
36
|
}, opts);
|
37
|
|
38
|
patterns.forEach(function (pattern, i) {
|
39
|
if (isNegative(pattern)) {
|
40
|
return;
|
41
|
}
|
42
|
|
43
|
var ignore = patterns.slice(i).filter(isNegative).map(function (pattern) {
|
44
|
return pattern.slice(1);
|
45
|
});
|
46
|
|
47
|
globTasks.push({
|
48
|
pattern: pattern,
|
49
|
opts: objectAssign({}, opts, {
|
50
|
ignore: opts.ignore.concat(ignore)
|
51
|
})
|
52
|
});
|
53
|
});
|
54
|
|
55
|
return globTasks;
|
56
|
}
|
57
|
|
58
|
module.exports = function (patterns, opts) {
|
59
|
var globTasks;
|
60
|
|
61
|
try {
|
62
|
globTasks = generateGlobTasks(patterns, opts);
|
63
|
} catch (err) {
|
64
|
return Promise.reject(err);
|
65
|
}
|
66
|
|
67
|
return Promise.all(globTasks.map(function (task) {
|
68
|
return globP(task.pattern, task.opts);
|
69
|
})).then(function (paths) {
|
70
|
return arrayUnion.apply(null, paths);
|
71
|
});
|
72
|
};
|
73
|
|
74
|
module.exports.sync = function (patterns, opts) {
|
75
|
var globTasks = generateGlobTasks(patterns, opts);
|
76
|
|
77
|
return globTasks.reduce(function (matches, task) {
|
78
|
return arrayUnion(matches, glob.sync(task.pattern, task.opts));
|
79
|
}, []);
|
80
|
};
|
81
|
|
82
|
module.exports.generateGlobTasks = generateGlobTasks;
|
83
|
|
84
|
module.exports.hasMagic = function (patterns, opts) {
|
85
|
return [].concat(patterns).some(function (pattern) {
|
86
|
return glob.hasMagic(pattern, opts);
|
87
|
});
|
88
|
};
|