-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathinternal-pattern-helper.ts
81 lines (68 loc) · 2.03 KB
/
internal-pattern-helper.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
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
import * as pathHelper from './internal-path-helper'
import {MatchKind} from './internal-match-kind'
import {Pattern} from './internal-pattern'
const IS_WINDOWS = process.platform === 'win32'
/**
* Given an array of patterns, returns an array of paths to search.
* Duplicates and paths under other included paths are filtered out.
*/
export function getSearchPaths(patterns: Pattern[]): string[] {
// Ignore negate patterns
patterns = patterns.filter(x => !x.negate)
// Create a map of all search paths
const searchPathMap: {[key: string]: string} = {}
for (const pattern of patterns) {
const key = IS_WINDOWS
? pattern.searchPath.toUpperCase()
: pattern.searchPath
searchPathMap[key] = 'candidate'
}
const result: string[] = []
for (const pattern of patterns) {
// Check if already included
const key = IS_WINDOWS
? pattern.searchPath.toUpperCase()
: pattern.searchPath
if (searchPathMap[key] === 'included') {
continue
}
// Check for an ancestor search path
let foundAncestor = false
let tempKey = key
let parent = pathHelper.dirname(tempKey)
while (parent !== tempKey) {
if (searchPathMap[parent]) {
foundAncestor = true
break
}
tempKey = parent
parent = pathHelper.dirname(tempKey)
}
// Include the search pattern in the result
if (!foundAncestor) {
result.push(pattern.searchPath)
searchPathMap[key] = 'included'
}
}
return result
}
/**
* Matches the patterns against the path
*/
export function match(patterns: Pattern[], itemPath: string): MatchKind {
let result: MatchKind = MatchKind.None
for (const pattern of patterns) {
if (pattern.negate) {
result &= ~pattern.match(itemPath)
} else {
result |= pattern.match(itemPath)
}
}
return result
}
/**
* Checks whether to descend further into the directory
*/
export function partialMatch(patterns: Pattern[], itemPath: string): boolean {
return patterns.some(x => !x.negate && x.partialMatch(itemPath))
}