@@ -15,6 +15,7 @@ class FileCache {
1515 #source;
1616 #directoryManager;
1717 #metadata;
18+ #defaultType;
1819 #contents;
1920 #dryRun = false ;
2021 #cacheDirectory = ".cache" ;
@@ -34,6 +35,12 @@ class FileCache {
3435 }
3536 }
3637
38+ setDefaultType ( type ) {
39+ if ( type ) {
40+ this . #defaultType = type ;
41+ }
42+ }
43+
3744 setDryRun ( val ) {
3845 this . #dryRun = Boolean ( val ) ;
3946 }
@@ -148,6 +155,10 @@ class FileCache {
148155 return existsCache . exists ( this . getContentsPath ( type ) ) ;
149156 }
150157
158+ getType ( ) {
159+ return this . #metadata?. type || this . #defaultType;
160+ }
161+
151162 getContents ( ) {
152163 if ( this . #contents) {
153164 return this . #contents;
@@ -157,7 +168,7 @@ class FileCache {
157168 // backwards compat with old caches
158169 if ( metadata ?. contents ) {
159170 // already parsed, part of the top level file
160- let normalizedContent = this . _backwardsCompatGetContents ( this . get ( ) , this . #metadata . type ) ;
171+ let normalizedContent = this . _backwardsCompatGetContents ( this . get ( ) , this . getType ( ) ) ;
161172 this . #contents = normalizedContent ;
162173 return normalizedContent ;
163174 }
@@ -176,8 +187,9 @@ class FileCache {
176187 // It is intentional to store contents in a separate file from the metadata: we don’t want to
177188 // have to read the entire contents via JSON.parse (or otherwise) to check the cache validity.
178189 this . #counts. read ++ ;
179- let data = fs . readFileSync ( this . contentsPath , null ) ;
180- if ( metadata ?. type === "json" || metadata ?. type === "parsed-xml" ) {
190+ let type = metadata ?. type || this . getType ( ) ;
191+ let data = fs . readFileSync ( this . contentsPath ) ;
192+ if ( type === "json" || type === "parsed-xml" ) {
181193 data = JSON . parse ( data ) ;
182194 }
183195 this . #contents = data ;
@@ -197,7 +209,8 @@ class FileCache {
197209 this . #counts. write ++ ;
198210 // the contents must exist before the cache metadata are saved below
199211 let contents = this . #contents;
200- if ( this . #metadata?. type === "json" || this . #metadata?. type === "parsed-xml" ) {
212+ let type = this . getType ( ) ;
213+ if ( type === "json" || type === "parsed-xml" ) {
201214 contents = JSON . stringify ( contents ) ;
202215 }
203216 fs . writeFileSync ( this . contentsPath , contents ) ;
0 commit comments