Fix: gracefully skip missing directories during remote deletion

This commit is contained in:
Ghaith Atfeh 2025-04-26 01:10:03 +03:00
parent 8e83cea867
commit 6b90cbdaf1

29
dist/index.js vendored
View File

@ -4632,17 +4632,26 @@ class Client {
* @example client.removeDir("/") // Remove everything. * @example client.removeDir("/") // Remove everything.
*/ */
async removeDir(remoteDirPath) { async removeDir(remoteDirPath) {
return this._exitAtCurrentDirectory(async () => { try {
await this.cd(remoteDirPath); return await this._exitAtCurrentDirectory(async () => {
// Get the absolute path of the target because remoteDirPath might be a relative path, even `../` is possible. await this.cd(remoteDirPath);
const absoluteDirPath = await this.pwd(); // Get the absolute path of the target because remoteDirPath might be a relative path, even `../` is possible.
await this.clearWorkingDir(); const absoluteDirPath = await this.pwd();
const dirIsRoot = absoluteDirPath === "/"; await this.clearWorkingDir();
if (!dirIsRoot) { const dirIsRoot = absoluteDirPath === "/";
await this.cdup(); if (!dirIsRoot) {
await this.removeEmptyDir(absoluteDirPath); 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 * Remove all files and directories in the working directory without removing