Skip to content

Commit

Permalink
Handle empty note subdirectories better (#32)
Browse files Browse the repository at this point in the history
Check directories in note directory more precisely
  • Loading branch information
EddieAbbondanzio authored Jan 7, 2023
1 parent 476f6b7 commit 98b8477
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
28 changes: 25 additions & 3 deletions src/main/ipc/plugins/notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export async function loadNotes(
const everyNote = (
await Promise.all(
entries
.filter(isNoteEntry)
.filter(e => isNoteSubdirectory(noteDirectoryPath, e))
.map(p => loadNoteFromFS(noteDirectoryPath, p.name)),
)
).map(n => ({ ...n, children: [] }));
Expand Down Expand Up @@ -350,6 +350,28 @@ export function splitNoteIntoFiles(note: NoteFile): [NoteMetadata, string] {
return [metadata, note.content];
}

export function isNoteEntry(entry: fs.Dirent): boolean {
return entry.isDirectory() && UUID_REGEX.test(entry.name);
export function isNoteSubdirectory(
noteDirectoryPath: string,
entry: fs.Dirent,
): boolean {
const isMatch = entry.isDirectory() && UUID_REGEX.test(entry.name);
if (!isMatch) {
return false;
}

const metadataPath = p.join(
noteDirectoryPath,
entry.name,
METADATA_FILE_NAME,
);
const markdownPath = p.join(
noteDirectoryPath,
entry.name,
MARKDOWN_FILE_NAME,
);

// Attachment directory doesn't need to be checked because it'll be re-created
// the first time the user attempts to add an attachment.

return fs.existsSync(metadataPath) && fs.existsSync(markdownPath);
}
31 changes: 23 additions & 8 deletions test/main/ipc/plugins/notes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
ATTACHMENTS_DIRECTORY,
buildNoteTree,
isNoteEntry,
isNoteSubdirectory,
loadNoteFromFS,
loadNotes,
MARKDOWN_FILE_NAME,
Expand Down Expand Up @@ -610,10 +610,25 @@ test("splitNoteIntoFiles", async () => {
});

test.each([
[{ isDirectory: () => false, name: "foo" } as fs.Dirent, false],
[{ isDirectory: () => true, name: "foo" } as fs.Dirent, false],
[{ isDirectory: () => false, name: uuid() } as fs.Dirent, false],
[{ isDirectory: () => true, name: uuid() } as fs.Dirent, true],
])("isNoteEntry", (entry, isNote) => {
expect(isNoteEntry(entry)).toBe(isNote);
});
["empty-text-file.txt", "", false],
["random-directory", {}, false],
[uuid(), "", false],
[uuid(), {}, false],
[uuid(), { [METADATA_FILE_NAME]: "" }, false],
[uuid(), { [MARKDOWN_FILE_NAME]: "" }, false],
[uuid(), { [METADATA_FILE_NAME]: "", [MARKDOWN_FILE_NAME]: "" }, true],
])(
"isNoteSubdirectory %s %s",
async (name: string, contents: any, isNote: boolean) => {
mockFS({
[FAKE_NOTE_DIRECTORY]: {
[name]: contents,
},
});

const entries = await fs.promises.readdir(FAKE_NOTE_DIRECTORY, {
withFileTypes: true,
});
expect(isNoteSubdirectory(FAKE_NOTE_DIRECTORY, entries[0])).toBe(isNote);
},
);

0 comments on commit 98b8477

Please sign in to comment.