Digested-Tile 2024-09-13
Authors:: Unknown License:: Unspecified Digest Root:: c82c2c8f23d9
MarkdownTile
ensureDirectoryExists.js
async function ensureDirectoryExists(path) {
// Resolve the path, expanding ~ to the user's home directory if necessary
let resolvedPath = path.startsWith('~')
? path.replace('~', app.vault.adapter.basePath)
: path;
// Split the path into individual directory names
const dirs = resolvedPath.split('/').filter(p => p.length);
// Start with the root directory
let currentPath = '';
// Iterate through each directory in the path
for (const dir of dirs) {
currentPath += dir + '/';
if (!(await app.vault.adapter.exists(currentPath))) {
try {
await app.vault.createFolder(currentPath);
console.log(`Created directory: ${currentPath}`);
} catch (error) {
console.error(`Error creating directory ${currentPath}:`, error);
throw error; // Re-throw the error to be caught by the calling function
}
}
}
console.log(`Ensured existence of directory: ${resolvedPath}`);
}
module.exports = ensureDirectoryExists;DeformattedTile
ensureDirectoryExists.js
async function ensureDirectoryExists(path) {
// Resolve the path, expanding ~ to the user's home directory if necessary
let resolvedPath = path.startsWith('~')
? path.replace('~', app.vault.adapter.basePath)
: path;
// Split the path into individual directory names
const dirs = resolvedPath.split('/').filter(p => p.length);
// Start with the root directory
let currentPath = '';
// Iterate through each directory in the path
for (const dir of dirs) {
currentPath += dir + '/';
if (!(await app.vault.adapter.exists(currentPath))) {
try {
await app.vault.createFolder(currentPath);
console.log(`Created directory: ${currentPath}`);
} catch (error) {
console.error(`Error creating directory ${currentPath}:`, error);
throw error; // Re-throw the error to be caught by the calling function
}
}
}
console.log(`Ensured existence of directory: ${resolvedPath}`);
}
module.exports = ensureDirectoryExists;