-
Notifications
You must be signed in to change notification settings - Fork 682
Expand file tree
/
Copy pathFileGlobSpecifier.ts
More file actions
200 lines (172 loc) · 5.81 KB
/
FileGlobSpecifier.ts
File metadata and controls
200 lines (172 loc) · 5.81 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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import type * as fs from 'node:fs';
import * as path from 'node:path';
import glob, { type FileSystemAdapter, type Entry } from 'fast-glob';
import { Async } from '@rushstack/node-core-library';
import type { IWatchFileSystemAdapter, IWatchedFileState } from '../utilities/WatchFileSystemAdapter';
import type { FileSelectionSpecifierBase as IFileSelectionSpecifier } from '../schemas/file-selection.schema.json.d.ts';
export type { IFileSelectionSpecifier };
/**
* A supported subset of options used when globbing files.
*
* @public
*/
export interface IGlobOptions {
/**
* Current working directory that the glob pattern will be applied to.
*/
cwd?: string;
/**
* Whether or not the returned file paths should be absolute.
*
* @defaultValue false
*/
absolute?: boolean;
/**
* Patterns to ignore when globbing.
*/
ignore?: string[];
/**
* Whether or not to include dot files when globbing.
*
* @defaultValue false
*/
dot?: boolean;
}
export interface IGetFileSelectionSpecifierPathsOptions {
fileGlobSpecifier: IFileSelectionSpecifier;
includeFolders?: boolean;
fileSystemAdapter?: FileSystemAdapter;
}
/**
* Glob a set of files and return a list of paths that match the provided patterns.
*
* @param patterns - Glob patterns to match against.
* @param options - Options that are used when globbing the set of files.
*
* @public
*/
export type GlobFn = (pattern: string | string[], options?: IGlobOptions | undefined) => Promise<string[]>;
/**
* Glob a set of files and return a map of paths that match the provided patterns to their current state in the watcher.
*
* @param patterns - Glob patterns to match against.
* @param options - Options that are used when globbing the set of files.
*
* @public
*/
export type WatchGlobFn = (
pattern: string | string[],
options?: IGlobOptions | undefined
) => Promise<Map<string, IWatchedFileState>>;
function isWatchFileSystemAdapter(adapter: FileSystemAdapter): adapter is IWatchFileSystemAdapter {
return !!(adapter as IWatchFileSystemAdapter).getStateAndTrackAsync;
}
export interface IWatchGlobOptions extends IGlobOptions {
fs: IWatchFileSystemAdapter;
}
export async function watchGlobAsync(
pattern: string | string[],
options: IWatchGlobOptions
): Promise<Map<string, IWatchedFileState>> {
const { fs, cwd, absolute } = options;
if (!cwd) {
throw new Error(`"cwd" must be set in the options passed to "watchGlobAsync"`);
}
const rawFiles: string[] = await glob(pattern, options);
const results: Map<string, IWatchedFileState> = new Map();
await Async.forEachAsync(
rawFiles,
async (file: string) => {
const state: IWatchedFileState = await fs.getStateAndTrackAsync(
absolute ? path.normalize(file) : path.resolve(cwd, file)
);
results.set(file, state);
},
{
concurrency: 20
}
);
return results;
}
export async function getFileSelectionSpecifierPathsAsync(
options: IGetFileSelectionSpecifierPathsOptions
): Promise<Map<string, fs.Dirent>> {
const { fileGlobSpecifier, includeFolders, fileSystemAdapter } = options;
const rawEntries: Entry[] = await glob(fileGlobSpecifier.includeGlobs!, {
fs: fileSystemAdapter,
cwd: fileGlobSpecifier.sourcePath,
ignore: fileGlobSpecifier.excludeGlobs,
onlyFiles: !includeFolders,
dot: true,
absolute: true,
objectMode: true
});
let results: Map<string, fs.Dirent>;
if (fileSystemAdapter && isWatchFileSystemAdapter(fileSystemAdapter)) {
results = new Map();
await Async.forEachAsync(
rawEntries,
async (entry: Entry) => {
const { path: filePath, dirent } = entry;
if (entry.dirent.isDirectory()) {
return;
}
const state: IWatchedFileState = await fileSystemAdapter.getStateAndTrackAsync(
path.normalize(filePath)
);
if (state.changed) {
results.set(filePath, dirent as fs.Dirent);
}
},
{
concurrency: 20
}
);
} else {
results = new Map(rawEntries.map((entry) => [entry.path, entry.dirent as fs.Dirent]));
}
return results;
}
export function asAbsoluteFileSelectionSpecifier<TSpecifier extends IFileSelectionSpecifier>(
rootPath: string,
fileGlobSpecifier: TSpecifier
): TSpecifier {
const { sourcePath } = fileGlobSpecifier;
return {
...fileGlobSpecifier,
sourcePath: sourcePath ? path.resolve(rootPath, sourcePath) : rootPath,
includeGlobs: getIncludedGlobPatterns(fileGlobSpecifier),
fileExtensions: undefined
};
}
function getIncludedGlobPatterns(fileGlobSpecifier: IFileSelectionSpecifier): string[] {
const patternsToGlob: Set<string> = new Set<string>();
// Glob file extensions with a specific glob to increase perf
const escapedFileExtensions: Set<string> = new Set<string>();
for (const fileExtension of fileGlobSpecifier.fileExtensions || []) {
let escapedFileExtension: string;
if (fileExtension.charAt(0) === '.') {
escapedFileExtension = fileExtension.slice(1);
} else {
escapedFileExtension = fileExtension;
}
escapedFileExtension = glob.escapePath(escapedFileExtension);
escapedFileExtensions.add(escapedFileExtension);
}
if (escapedFileExtensions.size > 1) {
patternsToGlob.add(`**/*.{${[...escapedFileExtensions].join(',')}}`);
} else if (escapedFileExtensions.size === 1) {
patternsToGlob.add(`**/*.${[...escapedFileExtensions][0]}`);
}
// Now include the other globs as well
for (const include of fileGlobSpecifier.includeGlobs || []) {
patternsToGlob.add(include);
}
// Include a default glob if none are specified
if (!patternsToGlob.size) {
patternsToGlob.add('**/*');
}
return [...patternsToGlob];
}