From 37cc19bf46d48517a2930f8ac0d06fd8733d30f9 Mon Sep 17 00:00:00 2001 From: Christopher Loverich <1010084+cloverich@users.noreply.github.com> Date: Mon, 9 Dec 2024 11:17:32 -0800 Subject: [PATCH] mkdirp edge case --- src/electron/ensureDir.js | 2 +- src/preload/files.ts | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/electron/ensureDir.js b/src/electron/ensureDir.js index 81496eb..55f1b28 100644 --- a/src/electron/ensureDir.js +++ b/src/electron/ensureDir.js @@ -4,7 +4,7 @@ const fs = require("fs"); * Borrowed from api files, since its typescript and this is not * Reconcile that later */ -exports.ensureDir = function ensureDir(directory) { +exports.ensureDir = function ensureDir(directory, create = true) { if (!directory) { throw new Error("ensureDir called with no directory path"); } diff --git a/src/preload/files.ts b/src/preload/files.ts index 1b1d0f6..e53e4c2 100644 --- a/src/preload/files.ts +++ b/src/preload/files.ts @@ -42,9 +42,15 @@ export class Files { try { await fs.promises.mkdir(dir, { recursive: true }); } catch (err) { - // If it already exists, good to go // note: ts can't find this type: instanceof ErrnoException if ((err as any).code === "EEXIST") { + // confirm it's a directory + const stats = await fs.promises.stat(dir); + if (!stats.isDirectory()) { + throw new Error(`[Files.mkdirp] ${dir} already exists as a file`); + } + + // already exists, good to go return dir; } else { throw err;