1+ import { cacheNamespace , createCacheKeyResolver , createOptionsHasher } from './cacheIdentity.ts' ;
2+ import { loadFmtCacheStore } from './cacheStore.ts' ;
3+ import type { FmtCacheEntry , FmtCacheStore } from './cacheStore.ts' ;
14import type {
5+ FmtFileCache ,
26 FmtExitCode ,
37 FmtFileRequest ,
48 FmtFileResult ,
@@ -11,6 +15,18 @@ import type { FmtWorkerPool } from './workerPool.ts';
1115type FormatFile = FmtWorkerPool [ 'formatFile' ] ;
1216type FmtFileOutcome = FmtFileResult | 'unchanged' | 'unsupported' ;
1317
18+ interface FmtFileRun {
19+ outcome : FmtFileOutcome ;
20+ key ?: string ;
21+ entry ?: FmtCacheEntry ;
22+ }
23+
24+ interface RunCache {
25+ store : FmtCacheStore ;
26+ resolveKey : ReturnType < typeof createCacheKeyResolver > ;
27+ hashOptions : ReturnType < typeof createOptionsHasher > ;
28+ }
29+
1430interface FmtWorkerPoolResult {
1531 files : FmtFileResult [ ] ;
1632 processedFileCount : number ;
@@ -32,22 +48,47 @@ const runFmtFile = async (
3248 file : FmtFileRequest ,
3349 shouldWrite : boolean ,
3450 formatFile : FormatFile ,
35- ) : Promise < FmtFileOutcome > => {
36- try {
37- const result = await formatFile ( file , shouldWrite ) ;
38- if ( result === 'unchanged' || result === 'unsupported' ) {
39- return result ;
51+ cache ?: RunCache ,
52+ ) : Promise < FmtFileRun > => {
53+ let key : string | undefined ;
54+ let fileCache : FmtFileCache | undefined ;
55+
56+ if ( cache ) {
57+ key = cache . resolveKey ( file . path ) ;
58+ if ( key !== undefined ) {
59+ const optionsHash = cache . hashOptions ( file . options ) ;
60+ if ( optionsHash === undefined ) {
61+ key = undefined ;
62+ } else {
63+ fileCache = {
64+ entry : cache . store . get ( key ) ,
65+ optionsHash,
66+ } ;
67+ }
4068 }
69+ }
4170
42- return {
43- path : file . path ,
44- status : shouldWrite ? 'written' : 'different' ,
45- } ;
71+ try {
72+ const result = await formatFile ( file , shouldWrite , fileCache ) ;
73+ const outcome : FmtFileOutcome =
74+ result . status === 'changed'
75+ ? {
76+ path : file . path ,
77+ status : shouldWrite ? 'written' : 'different' ,
78+ }
79+ : result . status ;
80+
81+ if ( key !== undefined && result . cacheEntry ) {
82+ return { outcome, key, entry : result . cacheEntry } ;
83+ }
84+ return { outcome } ;
4685 } catch ( error ) {
4786 return {
48- path : file . path ,
49- status : 'error' ,
50- error,
87+ outcome : {
88+ path : file . path ,
89+ status : 'error' ,
90+ error,
91+ } ,
5192 } ;
5293 }
5394} ;
@@ -57,7 +98,8 @@ const runPriorityFmtFiles = async (
5798 files : FmtFileRequest [ ] ,
5899 shouldWrite : boolean ,
59100 formatFile : FormatFile ,
60- ) : Promise < FmtFileOutcome [ ] > => {
101+ cache ?: RunCache ,
102+ ) : Promise < FmtFileRun [ ] > => {
61103 const priority : number [ ] = [ ] ;
62104 const rest : number [ ] = [ ] ;
63105
@@ -67,9 +109,9 @@ const runPriorityFmtFiles = async (
67109
68110 const order = priority . concat ( rest ) ;
69111 const outcomes = await Promise . all (
70- order . map ( ( index ) => runFmtFile ( files [ index ] , shouldWrite , formatFile ) ) ,
112+ order . map ( ( index ) => runFmtFile ( files [ index ] , shouldWrite , formatFile , cache ) ) ,
71113 ) ;
72- const results = new Array < FmtFileOutcome > ( files . length ) ;
114+ const results = new Array < FmtFileRun > ( files . length ) ;
73115 for ( let index = 0 ; index < order . length ; index ++ ) {
74116 results [ order [ index ] ] = outcomes [ index ] ;
75117 }
@@ -81,28 +123,32 @@ const runFmtFilesInWorkerPool = async (
81123 files : FmtFileRequest [ ] ,
82124 shouldWrite : boolean ,
83125 maxWorkers ?: number ,
126+ cache ?: RunCache ,
84127) : Promise < FmtWorkerPoolResult > => {
85128 const { createFmtWorkerPool } = await import ( './workerPool.ts' ) ;
86129 const workerPool = await createFmtWorkerPool ( files . length , maxWorkers ) ;
87130
88131 try {
89132 const results =
90133 workerPool . workerCount >= minPriorityWorkers
91- ? await runPriorityFmtFiles ( files , shouldWrite , workerPool . formatFile )
134+ ? await runPriorityFmtFiles ( files , shouldWrite , workerPool . formatFile , cache )
92135 : await Promise . all (
93- files . map ( ( file ) => runFmtFile ( file , shouldWrite , workerPool . formatFile ) ) ,
136+ files . map ( ( file ) => runFmtFile ( file , shouldWrite , workerPool . formatFile , cache ) ) ,
94137 ) ;
95138 const processedFiles : FmtFileResult [ ] = [ ] ;
96139 let processedFileCount = 0 ;
97140
98- for ( const result of results ) {
99- if ( result === 'unsupported' ) {
141+ for ( const { outcome, key, entry } of results ) {
142+ if ( key !== undefined && entry ) {
143+ cache ?. store . set ( key , entry ) ;
144+ }
145+ if ( outcome === 'unsupported' ) {
100146 continue ;
101147 }
102148
103149 processedFileCount ++ ;
104- if ( result !== 'unchanged' ) {
105- processedFiles . push ( result ) ;
150+ if ( outcome !== 'unchanged' ) {
151+ processedFiles . push ( outcome ) ;
106152 }
107153 }
108154
@@ -133,12 +179,23 @@ const runFmtFiles = async ({
133179 files,
134180 mode,
135181 maxWorkers,
182+ cache,
136183} : RunFmtFilesOptions ) : Promise < FmtRunResult > => {
137184 const shouldWrite = mode === 'write' ;
185+ let runCache : RunCache | undefined ;
186+ if ( files . length > 0 && cache && ! shouldWrite ) {
187+ runCache = {
188+ store : await loadFmtCacheStore ( cache . filePath , cacheNamespace ) ,
189+ resolveKey : createCacheKeyResolver ( cache . rootPath ) ,
190+ hashOptions : createOptionsHasher ( ) ,
191+ } ;
192+ }
193+
138194 const result =
139195 files . length === 0
140196 ? { files : [ ] , processedFileCount : 0 }
141- : await runFmtFilesInWorkerPool ( files , shouldWrite , maxWorkers ) ;
197+ : await runFmtFilesInWorkerPool ( files , shouldWrite , maxWorkers , runCache ) ;
198+ await runCache ?. store . save ( ) . catch ( ( ) => false ) ;
142199
143200 return {
144201 ...result ,
0 commit comments