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