Skip to content

Commit

Permalink
Adds directory manager to avoid duplicate mkdir calls
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 19, 2024
1 parent 22cda92 commit 9ae093f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
4 changes: 4 additions & 0 deletions eleventy-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const debug = require("debug")("Eleventy:Fetch");
const Sources = require("./src/Sources.js");
const RemoteAssetCache = require("./src/RemoteAssetCache.js");
const AssetCache = require("./src/AssetCache.js");
const DirectoryManager = require("./src/DirectoryManager.js");

const globalOptions = {
type: "buffer",
Expand Down Expand Up @@ -36,6 +37,8 @@ queue.on("active", () => {

let instCache = {};

let directoryManager = new DirectoryManager();

function createRemoteAssetCache(source, rawOptions = {}) {
if (!Sources.isFullUrl(source) && !Sources.isValidSource(source)) {
return Promise.reject(new Error("Invalid source. Received: " + source));
Expand All @@ -53,6 +56,7 @@ function createRemoteAssetCache(source, rawOptions = {}) {

let inst = new RemoteAssetCache(source, options.directory, options);
inst.setQueue(queue);
inst.setDirectoryManager(directoryManager);

instCache[sourceKey] = inst;

Expand Down
26 changes: 16 additions & 10 deletions src/AssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { createHash } = require("crypto");
const debugUtil = require("debug");

const Sources = require("./Sources.js");
const DirectoryManager = require("./DirectoryManager.js");

const debug = debugUtil("Eleventy:Fetch");
const debugAssets = debugUtil("Eleventy:Assets");
Expand All @@ -16,7 +17,7 @@ class AssetCache {
#cache;
#cacheDirectory;
#cacheLocationDirty = false;
#dirEnsured = false;
#directoryManager;
#rawContents = {}

constructor(source, cacheDirectory, options = {}) {
Expand Down Expand Up @@ -214,20 +215,21 @@ class AssetCache {
return `${this.cachePath}.${type}`;
}

get isDirEnsured() {
return this.#dirEnsured;
setDirectoryManager(manager) {
this.#directoryManager = manager;
}

ensureDir() {
if (this.options.dryRun || this.#dirEnsured) {
if (this.options.dryRun) {
return;
}

this.#dirEnsured = true;
if(!this.#directoryManager) {
// standalone fallback (for tests)
this.#directoryManager = new DirectoryManager();
}

fs.mkdirSync(this.cacheDirectory, {
recursive: true,
});
this.#directoryManager.create(this.cacheDirectory);
}

async save(contents, type = "buffer", metadata = {}) {
Expand Down Expand Up @@ -270,12 +272,16 @@ class AssetCache {

debug(`Fetching from cache ${contentPath}`);

if(this.source) {
debugAssets("[11ty/eleventy-fetch] Reading via %o", this.source);
} else {
debugAssets("[11ty/eleventy-fetch] Reading %o", contentPath);
}

if (type === "json" || type === "parsed-xml") {
debugAssets("[11ty/eleventy-fetch] Reading require('%o')", contentPath);
return require(contentPath);
}

debugAssets("[11ty/eleventy-fetch] Reading %o", contentPath);
return fs.readFileSync(contentPath, type !== "buffer" ? "utf8" : null);
}

Expand Down
24 changes: 24 additions & 0 deletions src/DirectoryManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fs = require("node:fs");
const path = require("node:path");
const debugUtil = require("debug");
const debugAssets = debugUtil("Eleventy:Assets");

class DirectoryManager {
#dirs = new Set();

isCreated(dir) {
return this.#dirs.has(dir);
}

create(dir) {
if(this.isCreated(dir)) {
return;
}

this.#dirs.add(dir);
debugAssets("[11ty/eleventy-fetch] Creating directory %o", dir);
fs.mkdirSync(dir, { recursive: true });
}
}

module.exports = DirectoryManager;

0 comments on commit 9ae093f

Please sign in to comment.