From 2a9a0355502cc7d7aed7b9c2bc77710d58eb035e Mon Sep 17 00:00:00 2001 From: Christopher Loverich <1010084+cloverich@users.noreply.github.com> Date: Mon, 9 Dec 2024 11:10:32 -0800 Subject: [PATCH] refactor: drop mkdirp --- package.json | 1 - src/electron/ensureDir.js | 7 +++++-- src/electron/userFilesInit.js | 1 - src/preload/client/files.ts | 18 ++++-------------- .../client/importer/FilesImportResolver.ts | 4 ++-- src/preload/client/types.ts | 4 ++-- src/preload/files.ts | 19 ++++++++++++++++--- 7 files changed, 29 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index bd55c01..218e6ac 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ "better-sqlite3": "^9.2.2", "electron-store": "^8.0.1", "knex": "^2.5.0", - "mkdirp": "^1.0.4", "uuidv7": "^0.6.3" }, "devDependencies": { diff --git a/src/electron/ensureDir.js b/src/electron/ensureDir.js index e23c109..81496eb 100644 --- a/src/electron/ensureDir.js +++ b/src/electron/ensureDir.js @@ -1,5 +1,4 @@ const fs = require("fs"); -const mkdirp = require("mkdirp"); /** * Borrowed from api files, since its typescript and this is not @@ -19,7 +18,11 @@ exports.ensureDir = function ensureDir(directory) { } } catch (err) { if (err.code !== "ENOENT") throw err; - mkdirp.sync(directory); + try { + fs.mkdirSync(directory, { recursive: true }); + } catch (err) { + if (err.code !== "EEXIST") throw err; + } } // NOTE: Documentation suggests Windows may report ok here, but then choke diff --git a/src/electron/userFilesInit.js b/src/electron/userFilesInit.js index 4204fb6..38f5bd4 100644 --- a/src/electron/userFilesInit.js +++ b/src/electron/userFilesInit.js @@ -1,6 +1,5 @@ const path = require("path"); const fs = require("fs"); -const mkdirp = require("mkdirp"); const settings = require("./settings"); const { ensureDir } = require("./ensureDir"); diff --git a/src/preload/client/files.ts b/src/preload/client/files.ts index cd44a79..eea781b 100644 --- a/src/preload/client/files.ts +++ b/src/preload/client/files.ts @@ -3,6 +3,7 @@ import Store from "electron-store"; import fs from "fs"; import path from "path"; import { uuidv7obj } from "uuidv7"; +import { Files } from "../files"; const { readFile, writeFile, access, stat } = fs.promises; interface UploadResponse { @@ -149,7 +150,7 @@ export class FilesClient { document.id, ); - await fs.promises.mkdir(journalPath, { recursive: true }); + await Files.mkdirp(journalPath); await fs.promises.writeFile(docPath, document.content); return docPath; }; @@ -171,18 +172,7 @@ export class FilesClient { createFolder = async (name: string) => { const baseDir = this.settings.get("NOTES_DIR") as string; const newPath = path.join(baseDir, name); - - try { - await fs.promises.mkdir(newPath, { 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") { - return newPath; - } else { - throw err; - } - } + await Files.mkdirp(newPath); }; removeFolder = async (name: string) => { @@ -257,7 +247,7 @@ export class FilesClient { } } catch (err: any) { if (err.code !== "ENOENT") throw err; - await fs.promises.mkdir(directory, { recursive: true }); + await Files.mkdirp(directory); } // NOTE: Documentation suggests Windows may report ok here, but then choke diff --git a/src/preload/client/importer/FilesImportResolver.ts b/src/preload/client/importer/FilesImportResolver.ts index 843c82d..bfccc83 100644 --- a/src/preload/client/importer/FilesImportResolver.ts +++ b/src/preload/client/importer/FilesImportResolver.ts @@ -4,7 +4,7 @@ import mdast from "mdast"; import path from "path"; import { uuidv7obj } from "uuidv7"; import { isNoteLink } from "../../../markdown"; -import { PathStatsFile } from "../../files"; +import { Files, PathStatsFile } from "../../files"; import { IFilesClient } from "../files"; const ATTACHMENTS_DIR = "_attachments"; @@ -226,7 +226,7 @@ export class FilesImportResolver { }); const attachmentsDir = path.join(chroniclesRoot, ATTACHMENTS_DIR); - await fs.promises.mkdir(attachmentsDir, { recursive: true }); + await Files.mkdirp(attachmentsDir); for await (const file of files) { const { sourcePathResolved, extension, chroniclesId } = file; diff --git a/src/preload/client/types.ts b/src/preload/client/types.ts index 20300bc..bc3f15e 100644 --- a/src/preload/client/types.ts +++ b/src/preload/client/types.ts @@ -127,16 +127,16 @@ export interface SaveRequest { createdAt?: string; updatedAt?: string; } + // Nobody would put node_modules in their note directory... right? // todo: Make this configurable - export const SKIPPABLE_FILES = new Set([ "node_modules", "dist", "build", "out", ]); + // Skip hidden folders and files, especially .git, .DS_Store, .Thumbs.db, etc // NOTE: This also skips _attachments, so add exclusion in importer routine - export const SKIPPABLE_PREFIXES = new Set([".", "_", "*", "~"]); diff --git a/src/preload/files.ts b/src/preload/files.ts index 1695e90..1b1d0f6 100644 --- a/src/preload/files.ts +++ b/src/preload/files.ts @@ -1,5 +1,4 @@ import fs, { Stats } from "fs"; -import mkdirp from "mkdirp"; import path from "path"; import { NotFoundError, ValidationError } from "./errors"; const { readFile, writeFile, access, stat } = fs.promises; @@ -39,6 +38,20 @@ export class Files { ); } + static async mkdirp(dir: string) { + 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") { + return dir; + } else { + throw err; + } + } + } + static async read(fp: string) { try { return await readFileStr(fp); @@ -69,7 +82,7 @@ export class Files { const fp = Files.pathForEntry(journalPath, date); const dir = path.parse(fp).dir; - await mkdirp(dir); + await Files.mkdirp(dir); return await writeFile(fp, contents); } @@ -154,7 +167,7 @@ export class Files { } } catch (err: any) { if (err.code !== "ENOENT") throw err; - await mkdirp(directory); + await Files.mkdirp(directory); } // NOTE: Documentation suggests Windows may report ok here, but then choke