@@ -15,6 +15,7 @@ class FileCache {
15
15
#source;
16
16
#directoryManager;
17
17
#metadata;
18
+ #defaultType;
18
19
#contents;
19
20
#dryRun = false ;
20
21
#cacheDirectory = ".cache" ;
@@ -34,6 +35,12 @@ class FileCache {
34
35
}
35
36
}
36
37
38
+ setDefaultType ( type ) {
39
+ if ( type ) {
40
+ this . #defaultType = type ;
41
+ }
42
+ }
43
+
37
44
setDryRun ( val ) {
38
45
this . #dryRun = Boolean ( val ) ;
39
46
}
@@ -148,6 +155,10 @@ class FileCache {
148
155
return existsCache . exists ( this . getContentsPath ( type ) ) ;
149
156
}
150
157
158
+ getType ( ) {
159
+ return this . #metadata?. type || this . #defaultType;
160
+ }
161
+
151
162
getContents ( ) {
152
163
if ( this . #contents) {
153
164
return this . #contents;
@@ -157,7 +168,7 @@ class FileCache {
157
168
// backwards compat with old caches
158
169
if ( metadata ?. contents ) {
159
170
// 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 ( ) ) ;
161
172
this . #contents = normalizedContent ;
162
173
return normalizedContent ;
163
174
}
@@ -176,8 +187,9 @@ class FileCache {
176
187
// It is intentional to store contents in a separate file from the metadata: we don’t want to
177
188
// have to read the entire contents via JSON.parse (or otherwise) to check the cache validity.
178
189
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" ) {
181
193
data = JSON . parse ( data ) ;
182
194
}
183
195
this . #contents = data ;
@@ -197,7 +209,8 @@ class FileCache {
197
209
this . #counts. write ++ ;
198
210
// the contents must exist before the cache metadata are saved below
199
211
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" ) {
201
214
contents = JSON . stringify ( contents ) ;
202
215
}
203
216
fs . writeFileSync ( this . contentsPath , contents ) ;
0 commit comments