|
1 | | -import type { FmtExitCode, FmtFileResult, FmtRunResult, RunFmtFilesOptions } from './types.ts'; |
| 1 | +import type { |
| 2 | + FmtExitCode, |
| 3 | + FmtFileRequest, |
| 4 | + FmtFileResult, |
| 5 | + FmtRunResult, |
| 6 | + RunFmtFilesOptions, |
| 7 | +} from './types.ts'; |
2 | 8 | import { formatFileSerial } from './serial.ts'; |
3 | 9 |
|
4 | | -const runFmtFiles = async ({ files, mode }: RunFmtFilesOptions): Promise<FmtRunResult> => { |
| 10 | +/** Formats one file and reports whether its contents differ. */ |
| 11 | +type FormatFile = (file: FmtFileRequest, shouldWrite: boolean) => Promise<boolean>; |
| 12 | + |
| 13 | +/** Converts a formatter outcome into the shared per-file result. */ |
| 14 | +const runFmtFile = async ( |
| 15 | + file: FmtFileRequest, |
| 16 | + shouldWrite: boolean, |
| 17 | + formatFile: FormatFile, |
| 18 | +): Promise<FmtFileResult> => { |
5 | 19 | const startTime = performance.now(); |
6 | | - const shouldWrite = mode === 'write'; |
| 20 | + |
| 21 | + try { |
| 22 | + const changed = await formatFile(file, shouldWrite); |
| 23 | + |
| 24 | + return { |
| 25 | + path: file.path, |
| 26 | + status: changed ? (shouldWrite ? 'written' : 'different') : 'unchanged', |
| 27 | + durationMs: performance.now() - startTime, |
| 28 | + }; |
| 29 | + } catch (error) { |
| 30 | + return { |
| 31 | + path: file.path, |
| 32 | + status: 'error', |
| 33 | + error, |
| 34 | + durationMs: performance.now() - startTime, |
| 35 | + }; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +/** Processes files sequentially while preserving input order. */ |
| 40 | +const runFmtFilesSerial = async ( |
| 41 | + files: FmtFileRequest[], |
| 42 | + shouldWrite: boolean, |
| 43 | +): Promise<FmtFileResult[]> => { |
7 | 44 | const results: FmtFileResult[] = []; |
| 45 | + |
| 46 | + for (const file of files) { |
| 47 | + results.push(await runFmtFile(file, shouldWrite, formatFileSerial)); |
| 48 | + } |
| 49 | + |
| 50 | + return results; |
| 51 | +}; |
| 52 | + |
| 53 | +/** Maps file results to the Prettier-compatible CLI exit code. */ |
| 54 | +const getFmtExitCode = (files: FmtFileResult[]): FmtExitCode => { |
8 | 55 | let exitCode: FmtExitCode = 0; |
9 | 56 |
|
10 | 57 | for (const file of files) { |
11 | | - const fileStartTime = performance.now(); |
12 | | - |
13 | | - try { |
14 | | - const changed = await formatFileSerial(file, shouldWrite); |
15 | | - |
16 | | - if (changed && !shouldWrite && exitCode === 0) { |
17 | | - exitCode = 1; |
18 | | - } |
19 | | - |
20 | | - results.push({ |
21 | | - path: file.path, |
22 | | - status: changed ? (shouldWrite ? 'written' : 'different') : 'unchanged', |
23 | | - durationMs: performance.now() - fileStartTime, |
24 | | - }); |
25 | | - } catch (error) { |
26 | | - exitCode = 2; |
27 | | - results.push({ |
28 | | - path: file.path, |
29 | | - status: 'error', |
30 | | - error, |
31 | | - durationMs: performance.now() - fileStartTime, |
32 | | - }); |
| 58 | + if (file.status === 'error') { |
| 59 | + return 2; |
| 60 | + } |
| 61 | + if (file.status === 'different') { |
| 62 | + exitCode = 1; |
33 | 63 | } |
34 | 64 | } |
35 | 65 |
|
| 66 | + return exitCode; |
| 67 | +}; |
| 68 | + |
| 69 | +/** Runs resolved files and summarizes their outcomes for the CLI. */ |
| 70 | +const runFmtFiles = async ({ files, mode }: RunFmtFilesOptions): Promise<FmtRunResult> => { |
| 71 | + const startTime = performance.now(); |
| 72 | + const results = await runFmtFilesSerial(files, mode === 'write'); |
| 73 | + |
36 | 74 | return { |
37 | 75 | files: results, |
38 | | - exitCode, |
| 76 | + exitCode: getFmtExitCode(results), |
39 | 77 | durationMs: performance.now() - startTime, |
40 | 78 | }; |
41 | 79 | }; |
|
0 commit comments