-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdefaultFilterHandler.ts
35 lines (30 loc) · 1.05 KB
/
defaultFilterHandler.ts
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
import pathUtils from 'path'
import { ExtOptions } from "../ExtOptions";
import { Entry, FilterHandler } from "../types";
import minimatch from 'minimatch'
/**
* Default filter handler that uses minimatch to accept/ignore files based on includeFilter and excludeFilter options.
*/
export const defaultFilterHandler: FilterHandler = (entry: Entry, relativePath: string, options: ExtOptions): boolean => {
const path = pathUtils.join(relativePath, entry.name)
if ((entry.stat.isFile() && options.includeFilter) && (!match(path, options.includeFilter))) {
return false
}
if ((options.excludeFilter) && (match(path, options.excludeFilter))) {
return false
}
return true
}
/**
* Matches path by pattern.
*/
function match(path: string, pattern: string): boolean {
const patternArray = pattern.split(',')
for (let i = 0; i < patternArray.length; i++) {
const pat = patternArray[i]
if (minimatch(path, pat, { dot: true, matchBase: true })) { //nocase
return true
}
}
return false
}