@@ -25,20 +25,28 @@ const formatFile = async ({
2525} : FormatFileTask ) : Promise < FmtWorkerResult > => {
2626 let source : string | undefined ;
2727 let contentHash : string | undefined ;
28+ const fileCache = shouldWrite ? undefined : cache ;
29+
30+ const readSource = ( ) : string => {
31+ if ( ! fileCache ) {
32+ return readFileSync ( file . path , 'utf8' ) ;
33+ }
2834
29- if ( cache && ! shouldWrite ) {
3035 const content = readFileSync ( file . path ) ;
3136 contentHash = hashContent ( content ) ;
32- source = content . toString ( 'utf8' ) ;
37+ return content . toString ( 'utf8' ) ;
38+ } ;
3339
34- const { entry, optionsHash } = cache ;
35- if ( entry ?. [ 0 ] === contentHash && entry [ 1 ] === optionsHash ) {
40+ if ( fileCache ?. entry && fileCache . entry [ 1 ] === fileCache . optionsHash ) {
41+ source = readSource ( ) ;
42+ const { entry } = fileCache ;
43+ if ( entry [ 0 ] === contentHash ) {
3644 return { status : entry [ 2 ] === 'clean' ? 'unchanged' : 'changed' } ;
3745 }
3846 }
3947
4048 const { formatFmtSource } = await import ( './format.ts' ) ;
41- const result = await formatFmtSource ( file , ( ) => ( source ??= readFileSync ( file . path , 'utf8' ) ) ) ;
49+ const result = await formatFmtSource ( file , ( ) => ( source ??= readSource ( ) ) ) ;
4250 if ( result . status === 'unsupported' ) {
4351 return { status : 'unsupported' } ;
4452 }
@@ -50,11 +58,15 @@ const formatFile = async ({
5058 }
5159
5260 const status = unchanged ? 'unchanged' : 'changed' ;
53- if ( ! cache || contentHash === undefined ) {
61+ if ( ! fileCache || contentHash === undefined ) {
5462 return { status } ;
5563 }
5664
57- const cacheEntry : FmtCacheEntry = [ contentHash , cache . optionsHash , unchanged ? 'clean' : 'dirty' ] ;
65+ const cacheEntry : FmtCacheEntry = [
66+ contentHash ,
67+ fileCache . optionsHash ,
68+ unchanged ? 'clean' : 'dirty' ,
69+ ] ;
5870 return { status, cacheEntry } ;
5971} ;
6072
0 commit comments