-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (59 loc) · 1.6 KB
/
index.js
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
/*!
* match-file <https://github.com/jonschlinkert/match-file>
*
* Copyright (c) 2016-2018, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
var path = require('path');
var isGlob = require('is-glob');
var endsWith = require('path-ends-with');
var mm = require('micromatch');
function matchFile(name, file) {
if (typeof name !== 'string') {
throw new TypeError('expected name to be a string');
}
if (!isVinyl(file)) {
throw new TypeError('expected file to be an object');
}
return file.key === name
|| file.path === name
|| file.history[0] === name
|| endsWith(file.history[0], name)
|| endsWith(file.path, name)
|| file.stem === name;
}
matchFile.matcher = function(pattern, options) {
if (typeof pattern !== 'string') {
throw new TypeError('expected pattern to be a string');
}
if (!isGlob(pattern)) {
return function(file) {
return matchFile(pattern, file);
};
}
var isMatch = mm.matcher(pattern, options);
return function(file) {
if (typeof file === 'string') {
return isMatch(file);
}
return matchFile(pattern, file)
|| isMatch(file.key)
|| isMatch(file.history[0])
|| isMatch(file.path)
|| isMatch(path.resolve(file.path))
|| isMatch(file.relative)
|| isMatch(file.basename)
|| isMatch(file.stem);
};
};
matchFile.isMatch = function(patterns, file, options) {
return matchFile.matcher(patterns, options)(file);
};
function isVinyl(file) {
return file && typeof file === 'object' && file._isVinyl === true;
}
/**
* Expose `matchFile`
*/
module.exports = matchFile;