-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobs.ts
More file actions
247 lines (227 loc) · 6.42 KB
/
globs.ts
File metadata and controls
247 lines (227 loc) · 6.42 KB
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
243
244
245
246
247
/**
* @fileoverview Glob pattern matching utilities with default ignore patterns.
* Provides file filtering and glob matcher functions for npm-like behavior.
*/
import type * as fastGlobType from './external/fast-glob.js'
import type picomatchType from './external/picomatch.js'
let _fastGlob: typeof fastGlobType | undefined
/*@__NO_SIDE_EFFECTS__*/
function getFastGlob() {
if (_fastGlob === undefined) {
_fastGlob = /*@__PURE__*/ require('./external/fast-glob.js')
}
return _fastGlob!
}
let _picomatch: typeof picomatchType | undefined
/*@__NO_SIDE_EFFECTS__*/
function getPicomatch() {
if (_picomatch === undefined) {
_picomatch = /*@__PURE__*/ require('./external/picomatch.js')
}
return _picomatch!
}
import { objectFreeze as ObjectFreeze } from './objects'
import {
LICENSE_GLOB,
LICENSE_GLOB_RECURSIVE,
LICENSE_ORIGINAL_GLOB_RECURSIVE,
} from './paths/globs'
// Type definitions
type Pattern = string
interface FastGlobOptions {
absolute?: boolean
baseNameMatch?: boolean
braceExpansion?: boolean
caseSensitiveMatch?: boolean
concurrency?: number
cwd?: string
deep?: number
dot?: boolean
extglob?: boolean
followSymbolicLinks?: boolean
fs?: unknown
globstar?: boolean
ignore?: string[]
ignoreFiles?: string[]
markDirectories?: boolean
objectMode?: boolean
onlyDirectories?: boolean
onlyFiles?: boolean
stats?: boolean
suppressErrors?: boolean
throwErrorOnBrokenSymbolicLink?: boolean
unique?: boolean
}
export interface GlobOptions extends FastGlobOptions {
ignoreOriginals?: boolean
recursive?: boolean
}
export type { Pattern, FastGlobOptions }
export const defaultIgnore = ObjectFreeze([
// Most of these ignored files can be included specifically if included in the
// files globs. Exceptions to this are:
// https://docs.npmjs.com/cli/v10/configuring-npm/package-json#files
// These can NOT be included.
// https://github.com/npm/npm-packlist/blob/v10.0.0/lib/index.js#L280
'**/.git',
'**/.npmrc',
// '**/bun.lockb?',
'**/node_modules',
// '**/package-lock.json',
// '**/pnpm-lock.ya?ml',
// '**/yarn.lock',
// Include npm-packlist defaults:
// https://github.com/npm/npm-packlist/blob/v10.0.0/lib/index.js#L15-L38
'**/.DS_Store',
'**/.gitignore',
'**/.hg',
'**/.lock-wscript',
'**/.npmignore',
'**/.svn',
'**/.wafpickle-*',
'**/.*.swp',
'**/._*/**',
'**/archived-packages/**',
'**/build/config.gypi',
'**/CVS',
'**/npm-debug.log',
'**/*.orig',
// Inline generic socket-registry .gitignore entries.
'**/.env',
'**/.eslintcache',
'**/.nvm',
'**/.tap',
'**/.vscode',
'**/*.tsbuildinfo',
'**/Thumbs.db',
// Inline additional ignores.
'**/bower_components',
])
/**
* Create a stream of license file paths matching glob patterns.
*/
/*@__NO_SIDE_EFFECTS__*/
export function globStreamLicenses(
dirname: string,
options?: GlobOptions,
): NodeJS.ReadableStream {
const {
ignore: ignoreOpt,
ignoreOriginals,
recursive,
...globOptions
} = { __proto__: null, ...options } as GlobOptions
const ignore = [
...(Array.isArray(ignoreOpt) ? ignoreOpt : defaultIgnore),
'**/*.{cjs,cts,js,json,mjs,mts,ts}',
]
if (ignoreOriginals) {
ignore.push(LICENSE_ORIGINAL_GLOB_RECURSIVE)
}
/* c8 ignore start - External fast-glob call */
const fastGlob = getFastGlob()
return fastGlob.globStream(
[recursive ? LICENSE_GLOB_RECURSIVE : LICENSE_GLOB],
{
__proto__: null,
absolute: true,
caseSensitiveMatch: false,
cwd: dirname,
...globOptions,
...(ignore ? { ignore } : {}),
} as import('fast-glob').Options,
)
/* c8 ignore stop */
}
const MATCHER_CACHE_MAX_SIZE = 100
const matcherCache = new Map<string, (path: string) => boolean>()
const matcherAccessOrder: string[] = []
function evictLRUMatcher() {
if (
matcherCache.size >= MATCHER_CACHE_MAX_SIZE &&
matcherAccessOrder.length > 0
) {
const oldest = matcherAccessOrder.shift()
if (oldest) {
matcherCache.delete(oldest)
}
}
}
/**
* Get a cached glob matcher function.
*/
/*@__NO_SIDE_EFFECTS__*/
export function getGlobMatcher(
glob: Pattern | Pattern[],
options?: { dot?: boolean; nocase?: boolean; ignore?: string[] },
): (path: string) => boolean {
const patterns = Array.isArray(glob) ? glob : [glob]
// Create stable cache key by sorting patterns and option keys
const sortedPatterns = [...patterns].sort()
const sortedOptions = options
? Object.keys(options)
.sort()
.map(k => `${k}:${JSON.stringify(options[k as keyof typeof options])}`)
.join(',')
: ''
const key = `${sortedPatterns.join('|')}:${sortedOptions}`
let matcher: ((path: string) => boolean) | undefined = matcherCache.get(key)
if (matcher) {
// Move to end of access order (LRU)
const index = matcherAccessOrder.indexOf(key)
if (index !== -1) {
matcherAccessOrder.splice(index, 1)
matcherAccessOrder.push(key)
}
return matcher
}
// Evict oldest entry if cache is full
evictLRUMatcher()
// Separate positive and negative patterns.
const positivePatterns = patterns.filter(p => !p.startsWith('!'))
const negativePatterns = patterns
.filter(p => p.startsWith('!'))
.map(p => p.slice(1))
// Use ignore option for negation patterns.
const matchOptions = {
dot: true,
nocase: true,
...options,
...(negativePatterns.length > 0 ? { ignore: negativePatterns } : {}),
}
/* c8 ignore next 5 - External picomatch call */
const picomatch = getPicomatch()
matcher = picomatch(
positivePatterns.length > 0 ? positivePatterns : patterns,
matchOptions,
) as (path: string) => boolean
matcherCache.set(key, matcher)
matcherAccessOrder.push(key)
return matcher
}
/**
* Asynchronously find files matching glob patterns.
* Wrapper around fast-glob.
*/
/*@__NO_SIDE_EFFECTS__*/
export function glob(
patterns: Pattern | Pattern[],
options?: FastGlobOptions,
): Promise<string[]> {
/* c8 ignore next - External fast-glob call */
const fastGlob = getFastGlob()
return fastGlob.glob(patterns, options as import('fast-glob').Options)
}
/**
* Synchronously find files matching glob patterns.
* Wrapper around fast-glob.sync.
*/
/*@__NO_SIDE_EFFECTS__*/
export function globSync(
patterns: Pattern | Pattern[],
options?: FastGlobOptions,
): string[] {
/* c8 ignore next - External fast-glob call */
const fastGlob = getFastGlob()
return fastGlob.globSync(patterns, options as import('fast-glob').Options)
}