-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathinternal-globber.ts
242 lines (210 loc) · 6.43 KB
/
internal-globber.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import * as core from '@actions/core'
import * as fs from 'fs'
import * as globOptionsHelper from './internal-glob-options-helper'
import * as path from 'path'
import * as patternHelper from './internal-pattern-helper'
import {GlobOptions} from './internal-glob-options'
import {MatchKind} from './internal-match-kind'
import {Pattern} from './internal-pattern'
import {SearchState} from './internal-search-state'
const IS_WINDOWS = process.platform === 'win32'
export {GlobOptions}
/**
* Used to match files and directories
*/
export interface Globber {
/**
* Returns the search path preceding the first glob segment, from each pattern.
* Duplicates and descendants of other paths are filtered out.
*
* Example 1: The patterns `/foo/*` and `/bar/*` returns `/foo` and `/bar`.
*
* Example 2: The patterns `/foo/*` and `/foo/bar/*` returns `/foo`.
*/
getSearchPaths(): string[]
/**
* Returns files and directories matching the glob patterns.
*
* Order of the results is not guaranteed.
*/
glob(): Promise<string[]>
/**
* Returns files and directories matching the glob patterns.
*
* Order of the results is not guaranteed.
*/
globGenerator(): AsyncGenerator<string, void>
}
export class DefaultGlobber implements Globber {
private readonly options: GlobOptions
private readonly patterns: Pattern[] = []
private readonly searchPaths: string[] = []
private constructor(options?: GlobOptions) {
this.options = globOptionsHelper.getOptions(options)
}
getSearchPaths(): string[] {
// Return a copy
return this.searchPaths.slice()
}
async glob(): Promise<string[]> {
const result: string[] = []
for await (const itemPath of this.globGenerator()) {
result.push(itemPath)
}
return result
}
async *globGenerator(): AsyncGenerator<string, void> {
// Fill in defaults options
const options = globOptionsHelper.getOptions(this.options)
// Implicit descendants?
const patterns: Pattern[] = []
for (const pattern of this.patterns) {
patterns.push(pattern)
if (
options.implicitDescendants &&
(pattern.trailingSeparator ||
pattern.segments[pattern.segments.length - 1] !== '**')
) {
patterns.push(
new Pattern(pattern.negate, pattern.segments.concat('**'))
)
}
}
// Push the search paths
const stack: SearchState[] = []
for (const searchPath of patternHelper.getSearchPaths(patterns)) {
core.debug(`Search path '${searchPath}'`)
// Exists?
try {
// Intentionally using lstat. Detection for broken symlink
// will be performed later (if following symlinks).
await fs.promises.lstat(searchPath)
} catch (err) {
if (err.code === 'ENOENT') {
continue
}
throw err
}
stack.unshift(new SearchState(searchPath, 1))
}
// Search
const traversalChain: string[] = [] // used to detect cycles
while (stack.length) {
// Pop
const item = stack.pop() as SearchState
// Match?
const match = patternHelper.match(patterns, item.path)
const partialMatch =
!!match || patternHelper.partialMatch(patterns, item.path)
if (!match && !partialMatch) {
continue
}
// Stat
const stats: fs.Stats | undefined = await DefaultGlobber.stat(
item,
options,
traversalChain
)
// Broken symlink, or symlink cycle detected, or no longer exists
if (!stats) {
continue
}
// Directory
if (stats.isDirectory()) {
// Matched
if (match & MatchKind.Directory) {
yield item.path
}
// Descend?
else if (!partialMatch) {
continue
}
// Push the child items in reverse
const childLevel = item.level + 1
const childItems = (await fs.promises.readdir(item.path)).map(
x => new SearchState(path.join(item.path, x), childLevel)
)
stack.push(...childItems.reverse())
}
// File
else if (match & MatchKind.File) {
yield item.path
}
}
}
/**
* Constructs a DefaultGlobber
*/
static async create(
patterns: string,
options?: GlobOptions
): Promise<DefaultGlobber> {
const result = new DefaultGlobber(options)
if (IS_WINDOWS) {
patterns = patterns.replace(/\r\n/g, '\n')
patterns = patterns.replace(/\r/g, '\n')
}
const lines = patterns.split('\n').map(x => x.trim())
for (const line of lines) {
// Empty or comment
if (!line || line.startsWith('#')) {
continue
}
// Pattern
else {
result.patterns.push(new Pattern(line))
}
}
result.searchPaths.push(...patternHelper.getSearchPaths(result.patterns))
return result
}
private static async stat(
item: SearchState,
options: GlobOptions,
traversalChain: string[]
): Promise<fs.Stats | undefined> {
// Note:
// `stat` returns info about the target of a symlink (or symlink chain)
// `lstat` returns info about a symlink itself
let stats: fs.Stats
if (options.followSymbolicLinks) {
try {
// Use `stat` (following symlinks)
stats = await fs.promises.stat(item.path)
} catch (err) {
if (err.code === 'ENOENT') {
if (options.omitBrokenSymbolicLinks) {
core.debug(`Broken symlink '${item.path}'`)
return undefined
}
throw new Error(
`No information found for the path '${item.path}'. This may indicate a broken symbolic link.`
)
}
throw err
}
} else {
// Use `lstat` (not following symlinks)
stats = await fs.promises.lstat(item.path)
}
// Note, isDirectory() returns false for the lstat of a symlink
if (stats.isDirectory() && options.followSymbolicLinks) {
// Get the realpath
const realPath: string = await fs.promises.realpath(item.path)
// Fixup the traversal chain to match the item level
while (traversalChain.length >= item.level) {
traversalChain.pop()
}
// Test for a cycle
if (traversalChain.some((x: string) => x === realPath)) {
core.debug(
`Symlink cycle detected for path '${item.path}' and realpath '${realPath}'`
)
return undefined
}
// Update the traversal chain
traversalChain.push(realPath)
}
return stats
}
}