From e7aba4d6e27831ff0d15a9dad489d7aad5d63656 Mon Sep 17 00:00:00 2001 From: RandomFractals Date: Sun, 4 Jun 2023 09:21:36 -0500 Subject: [PATCH] add `folderExists` to `fsUtils.ts` to check if local template folder exists (#62) and use it in `deleteFolder` in `fsUtils.ts` too --- src/utils/fsUtils.ts | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/utils/fsUtils.ts b/src/utils/fsUtils.ts index 2365d1e..f5a0d87 100644 --- a/src/utils/fsUtils.ts +++ b/src/utils/fsUtils.ts @@ -5,7 +5,26 @@ import { } from 'vscode'; /** - * Deletes a folder from the open project workspace. + * Checks if the given folder exists using workspace.fs API. + * + * @param folder Folder Uri. + */ +export async function folderExists(folder: Uri): Promise { + try { + const fileStat = await workspace.fs.stat(folder); + if (fileStat.type === FileType.Directory) { + return true; + } + } + catch (error) { + return false; + } + return false; +} + +/** + * Deletes a folder from the open project workspace + * ussing workspace.fs API. * * @param relativeFolderPath Relative folder path to delete. * @returns True if the folder is deleted, and false otherwise. @@ -15,14 +34,14 @@ export async function deleteFolder(relativeFolderPath: string): Promise if (workspaceFolders) { for (const folder of workspaceFolders) { const folderUri: Uri = Uri.joinPath(folder.uri, relativeFolderPath); - try { - const fileStat = await workspace.fs.stat(folderUri); - if (fileStat.type === FileType.Directory) { - await workspace.fs.delete(folderUri, {recursive: true}); + if (await folderExists(folderUri)) { + try { + await workspace.fs.delete(folderUri, { recursive: true }); return true; } - } - catch (error) { + catch (error) { + return false; + } } } }