Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions apps/obsidian/src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import { TAbstractFile, TFolder, Vault, normalizePath } from "obsidian";
import { Vault, normalizePath } from "obsidian";

export const checkAndCreateFolder = async (folderpath: string, vault: Vault) => {
export const checkAndCreateFolder = async (
folderpath: string,
vault: Vault,
) => {
if (!folderpath) return;
const normalizedPath = normalizePath(folderpath);

const abstractItem = vault.getAbstractFileByPath(normalizedPath);
if (abstractItem instanceof TFolder) return;
if (abstractItem instanceof TAbstractFile) {
throw new Error(`${normalizedPath} exists as a file`);
const existingFolder = await vault.adapter.exists(normalizedPath, false);
if (existingFolder) return;

try {
await vault.createFolder(normalizedPath);
} catch (e) {
const message = e instanceof Error ? e.message : String(e);
if (
message.includes("Folder already exists") ||
message.includes("already exists")
) {
return;
}
throw e;
}
await vault.createFolder(normalizedPath);
};

export const getNewUniqueFilepath = ({
Expand Down