Skip to content

Commit

Permalink
refactor: drop mkdirp
Browse files Browse the repository at this point in the history
  • Loading branch information
cloverich committed Dec 9, 2024
1 parent 52fa08c commit 2a9a035
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
7 changes: 5 additions & 2 deletions src/electron/ensureDir.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const fs = require("fs");
const mkdirp = require("mkdirp");

/**
* Borrowed from api files, since its typescript and this is not
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/electron/userFilesInit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const path = require("path");
const fs = require("fs");
const mkdirp = require("mkdirp");
const settings = require("./settings");
const { ensureDir } = require("./ensureDir");

Expand Down
18 changes: 4 additions & 14 deletions src/preload/client/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
};
Expand All @@ -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) => {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/preload/client/importer/FilesImportResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/preload/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([".", "_", "*", "~"]);
19 changes: 16 additions & 3 deletions src/preload/files.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2a9a035

Please sign in to comment.