-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunner.ts
More file actions
299 lines (267 loc) · 8 KB
/
Copy pathrunner.ts
File metadata and controls
299 lines (267 loc) · 8 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import {
cacheNamespace,
createCacheKeyResolver,
createOptionsHasher,
} from './cacheIdentity.ts';
import { loadFmtCacheStore } from './cacheStore.ts';
import type { FmtCacheEntry, FmtCacheStore } from './cacheStore.ts';
import { hasDottedBasename } from './pathHelpers.ts';
import type {
FmtFileCache,
FmtExitCode,
FmtFileRequest,
FmtFileResult,
FmtPluginSpecifier,
FmtRunResult,
RunFmtFilesOptions,
} from './types.ts';
import type { FmtWorkerPool } from './workerPool.ts';
/** Formats one file and reports whether its contents differ. */
type FormatFile = FmtWorkerPool['formatFile'];
type FmtFileOutcome = FmtFileResult | 'unchanged' | 'unsupported';
interface FmtFileRun {
outcome: FmtFileOutcome;
key?: string;
entry?: FmtCacheEntry;
}
interface FmtFileRunTask {
file: FmtFileRequest;
key?: string;
cache?: FmtFileCache;
}
interface RunCache {
store: FmtCacheStore;
resolveKey: ReturnType<typeof createCacheKeyResolver>;
hashOptions: ReturnType<typeof createOptionsHasher>;
}
interface FmtWorkerPoolResult {
files: FmtFileResult[];
processedFileCount: number;
}
/** Benchmarks show stable scheduling gains only when at least eight workers share the queue. */
const minPriorityWorkers = 8;
/**
* Markdown parsing is consistently slower in representative repositories. Keep this signal
* narrow: parser overrides and file size can outweigh the extension, and deferring every JS/TS
* file could turn a large source file into the final straggler.
*/
const isMarkdown = (file: FmtFileRequest): boolean =>
file.path.endsWith('.md') || file.path.endsWith('.mdx');
/** Resolves each distinct plugin once before the synchronous per-file cache path. */
const loadPluginFingerprints = async (
files: FmtFileRequest[],
): Promise<Map<string, string> | undefined> => {
const plugins = new Map<string, FmtPluginSpecifier>();
for (const file of files) {
try {
for (const plugin of file.options.plugins ?? []) {
if (typeof plugin === 'string' || plugin instanceof URL) {
plugins.set(plugin instanceof URL ? plugin.href : plugin, plugin);
}
}
} catch {
// Unreadable options cannot be cached by the options hasher.
}
}
if (plugins.size === 0) {
return undefined;
}
const { createFingerprintResolver } = await import(
/* rspackChunkName: 'fmtPlugins' */
'./plugins.ts'
);
const resolveFingerprint = createFingerprintResolver();
const entries = await Promise.all(
Array.from(
plugins,
async ([key, plugin]) => [key, await resolveFingerprint(plugin)] as const,
),
);
const fingerprints = new Map<string, string>();
for (const [key, fingerprint] of entries) {
if (fingerprint !== undefined) {
fingerprints.set(key, fingerprint);
}
}
return fingerprints;
};
/** Resolves the portable cache identity before work is dispatched. */
const createRunTask = (
file: FmtFileRequest,
cache?: RunCache,
): FmtFileRunTask => {
let key: string | undefined;
let fileCache: FmtFileCache | undefined;
if (cache) {
key = cache.resolveKey(file.path);
if (key !== undefined) {
const optionsHash = cache.hashOptions(file.options);
if (optionsHash === undefined) {
key = undefined;
} else {
fileCache = {
entry: cache.store.get(key),
optionsHash,
};
}
}
}
return { file, key, cache: fileCache };
};
const isCachedUnsupported = ({ file, cache }: FmtFileRunTask): boolean => {
if (!cache?.entry) {
return false;
}
return (
cache.entry[0] === '' &&
cache.entry[1] === cache.optionsHash &&
cache.entry[2] === 'unsupported' &&
hasDottedBasename(file.path)
);
};
/** Converts a formatter outcome into the shared per-file result. */
const runFmtFile = async (
task: FmtFileRunTask,
shouldWrite: boolean,
formatFile: FormatFile,
): Promise<FmtFileRun> => {
if (isCachedUnsupported(task)) {
return { outcome: 'unsupported' };
}
const { file, key, cache } = task;
try {
const result = await formatFile(file, shouldWrite, cache);
const outcome: FmtFileOutcome =
result.status === 'changed'
? {
path: file.path,
status: shouldWrite ? 'written' : 'different',
}
: result.status;
if (key !== undefined && result.cacheEntry) {
return { outcome, key, entry: result.cacheEntry };
}
return { outcome };
} catch (error) {
return {
outcome: {
path: file.path,
status: 'error',
error,
},
};
}
};
/** Starts slower Markdown parsers first while preserving order within both priority groups. */
const runPriorityTasks = async (
tasks: FmtFileRunTask[],
shouldWrite: boolean,
formatFile: FormatFile,
): Promise<FmtFileRun[]> => {
const priority: number[] = [];
const rest: number[] = [];
for (let index = 0; index < tasks.length; index++) {
(isMarkdown(tasks[index].file) ? priority : rest).push(index);
}
const order = priority.concat(rest);
const outcomes = await Promise.all(
order.map((index) => runFmtFile(tasks[index], shouldWrite, formatFile)),
);
const results = new Array<FmtFileRun>(tasks.length);
for (let index = 0; index < order.length; index++) {
results[order[index]] = outcomes[index];
}
return results;
};
/** Processes files in a worker pool while preserving input order. */
const runWithWorkers = async (
files: FmtFileRequest[],
shouldWrite: boolean,
maxWorkers?: number,
cache?: RunCache,
): Promise<FmtWorkerPoolResult> => {
const tasks = files.map((file) => createRunTask(file, cache));
const pendingFileCount = tasks.reduce(
(count, task) => count + (isCachedUnsupported(task) ? 0 : 1),
0,
);
if (pendingFileCount === 0) {
return { files: [], processedFileCount: 0 };
}
const { createWorkerPool } = await import('./workerPool.ts');
const workerPool = await createWorkerPool(pendingFileCount, maxWorkers);
try {
const results =
workerPool.workerCount >= minPriorityWorkers
? await runPriorityTasks(tasks, shouldWrite, workerPool.formatFile)
: await Promise.all(
tasks.map((task) =>
runFmtFile(task, shouldWrite, workerPool.formatFile),
),
);
const processedFiles: FmtFileResult[] = [];
let processedFileCount = 0;
for (const { outcome, key, entry } of results) {
if (key !== undefined && entry) {
cache?.store.set(key, entry);
}
if (outcome === 'unsupported') {
continue;
}
processedFileCount++;
if (outcome !== 'unchanged') {
processedFiles.push(outcome);
}
}
return { files: processedFiles, processedFileCount };
} finally {
await workerPool.terminate();
}
};
/** Maps file results to the Prettier-compatible CLI exit code. */
const getExitCode = (files: FmtFileResult[]): FmtExitCode => {
let exitCode: FmtExitCode = 0;
for (const file of files) {
if (file.status === 'error') {
return 2;
}
if (file.status === 'different') {
exitCode = 1;
}
}
return exitCode;
};
/** Runs resolved files and summarizes their outcomes for the CLI. */
const runFmtFiles = async ({
files,
mode,
maxWorkers,
cache,
}: RunFmtFilesOptions): Promise<FmtRunResult> => {
const shouldWrite = mode === 'write';
let runCache: RunCache | undefined;
if (files.length > 0 && cache) {
const [store, fingerprints] = await Promise.all([
loadFmtCacheStore(cache.filePath, cacheNamespace),
loadPluginFingerprints(files),
]);
runCache = {
store,
resolveKey: createCacheKeyResolver(cache.rootPath),
hashOptions: createOptionsHasher(fingerprints),
};
}
const result =
files.length === 0
? { files: [], processedFileCount: 0 }
: await runWithWorkers(files, shouldWrite, maxWorkers, runCache);
await runCache?.store.save().catch(() => false);
return {
...result,
exitCode:
files.length > 0 && result.processedFileCount === 0
? 2
: getExitCode(result.files),
};
};
export { runFmtFiles };