From 6b90cbdaf1b545fc01e063c4d59b76aee9169337 Mon Sep 17 00:00:00 2001 From: Ghaith Atfeh <85496434+ghaithatfeh@users.noreply.github.com> Date: Sat, 26 Apr 2025 01:10:03 +0300 Subject: [PATCH] Fix: gracefully skip missing directories during remote deletion --- dist/index.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/dist/index.js b/dist/index.js index fa7c245..b74c217 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4632,17 +4632,26 @@ class Client { * @example client.removeDir("/") // Remove everything. */ async removeDir(remoteDirPath) { - return this._exitAtCurrentDirectory(async () => { - await this.cd(remoteDirPath); - // Get the absolute path of the target because remoteDirPath might be a relative path, even `../` is possible. - const absoluteDirPath = await this.pwd(); - await this.clearWorkingDir(); - const dirIsRoot = absoluteDirPath === "/"; - if (!dirIsRoot) { - await this.cdup(); - await this.removeEmptyDir(absoluteDirPath); + try { + return await this._exitAtCurrentDirectory(async () => { + await this.cd(remoteDirPath); + // Get the absolute path of the target because remoteDirPath might be a relative path, even `../` is possible. + const absoluteDirPath = await this.pwd(); + await this.clearWorkingDir(); + const dirIsRoot = absoluteDirPath === "/"; + if (!dirIsRoot) { + await this.cdup(); + await this.removeEmptyDir(absoluteDirPath); + } + }); + } catch (err) { + // Safely ignore if the folder doesn't exist + if (err.code === 550 || err.message.includes("No such file or directory") || err.message.includes("File not found")) { + console.warn(`Warning: Folder "${remoteDirPath}" not found, skipping deletion.`); + return; } - }); + throw err; + } } /** * Remove all files and directories in the working directory without removing