Skip to content

Commit 431c585

Browse files
committed
Ignore setInitialCacheTimestamp if called after initial save (no error)
1 parent 8dc464b commit 431c585

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

src/AssetCache.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class AssetCache {
1111
#source;
1212
#hash;
1313
#customFilename;
14-
#hasSaved = false;
1514
#cache;
1615
#cacheDirectory;
1716
#cacheLocationDirty = false;
@@ -42,10 +41,6 @@ class AssetCache {
4241

4342
setInitialCacheTimestamp(timestamp) {
4443
this.initialCacheTimestamp = timestamp;
45-
46-
if(this.#hasSaved) {
47-
throw new Error("`setInitialCacheTimestamp` method must be called before the object is saved.");
48-
}
4944
}
5045

5146
log(message) {
@@ -235,21 +230,11 @@ class AssetCache {
235230
}
236231

237232
async save(contents, type = "buffer", metadata = {}) {
238-
if (this.options.dryRun) {
239-
debug("An attempt was made to save to the file system with `dryRun: true`. Skipping.");
240-
241-
// Errors are still expected from this
242-
this.#hasSaved = true;
243-
return;
244-
}
245-
246233
if(!contents) {
247-
throw new Error("save(contents) expects contents (it was falsy)");
234+
throw new Error("save(contents) expects contents (was falsy)");
248235
}
249236

250-
if(!this.isDirEnsured) {
251-
this.ensureDir();
252-
}
237+
this.ensureDir();
253238

254239
if (type === "json" || type === "parsed-xml") {
255240
contents = JSON.stringify(contents);
@@ -258,18 +243,22 @@ class AssetCache {
258243
let contentPath = this.getCachedContentsPath(type);
259244

260245
// the contents must exist before the cache metadata are saved below
261-
fs.writeFileSync(contentPath, contents);
262-
263-
debug(`Writing ${contentPath}`);
246+
if(!this.options.dryRun) {
247+
fs.writeFileSync(contentPath, contents);
248+
debug(`Writing ${contentPath}`);
249+
} else {
250+
debug(`Dry run writing ${contentPath}`);
251+
}
264252

265253
this.cache.set(this.hash, {
266254
cachedAt: this.initialCacheTimestamp || Date.now(),
267255
type: type,
268256
metadata,
269257
});
270258

271-
this.cache.save();
272-
this.#hasSaved = true;
259+
if(!this.options.dryRun) {
260+
this.cache.save();
261+
}
273262
}
274263

275264
async getCachedContents(type) {

test/AssetCacheTest.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,25 +142,40 @@ test("setInitialCacheTimestamp method (used by Eleventy Image to establish a con
142142
let cache = new AssetCache("this_is_a_test", ".cache", {
143143
dryRun: true
144144
});
145-
let timestamp = Date.now();
145+
let timestamp = (new Date(2024,1,1)).getTime();
146146
cache.setInitialCacheTimestamp(timestamp);
147147

148148
await cache.save("test");
149149

150150
t.is(cache.getCachedTimestamp(), timestamp);
151151
});
152152

153-
test("setInitialCacheTimestamp method after save throws error", async (t) => {
154-
let cache = new AssetCache("this_is_a_test", ".cache", {
153+
test("setInitialCacheTimestamp method after save is ignored", async (t) => {
154+
let cache = new AssetCache("this_is_a_test2", ".cache", {
155155
dryRun: true
156156
});
157-
let timestamp = Date.now();
157+
158+
let timestamp = (new Date(2024,1,1)).getTime();
158159

159160
await cache.save("test");
160161

161-
t.throws(() => {
162-
cache.setInitialCacheTimestamp(timestamp);
163-
}, {
164-
message: "`setInitialCacheTimestamp` method must be called before the object is saved."
165-
})
162+
cache.setInitialCacheTimestamp(timestamp);
163+
164+
t.not(cache.getCachedTimestamp(), timestamp);
165+
});
166+
167+
test("setInitialCacheTimestamp method after second save is used", async (t) => {
168+
let cache = new AssetCache("this_is_a_test3", ".cache", {
169+
dryRun: true
170+
});
171+
172+
let timestamp = (new Date(2024,1,1)).getTime();
173+
174+
await cache.save("test");
175+
176+
cache.setInitialCacheTimestamp(timestamp);
177+
178+
await cache.save("test");
179+
180+
t.is(cache.getCachedTimestamp(), timestamp);
166181
});

0 commit comments

Comments
 (0)