From 941436dd066d5f06a5a34751a647960fb045c2e2 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sat, 11 Jan 2025 11:49:25 +0800 Subject: [PATCH] Make delete more safer --- yazi-cli/src/package/delete.rs | 44 +++++++++++++++++++++++++++++++--- yazi-cli/src/package/deploy.rs | 21 +++------------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/yazi-cli/src/package/delete.rs b/yazi-cli/src/package/delete.rs index a0570b37..f9f2e9ae 100644 --- a/yazi-cli/src/package/delete.rs +++ b/yazi-cli/src/package/delete.rs @@ -1,6 +1,6 @@ -use anyhow::{Result, bail}; +use anyhow::{Context, Result, bail}; use tokio::fs; -use yazi_fs::maybe_exists; +use yazi_fs::{maybe_exists, ok_or_not_found, remove_dir_clean}; use yazi_macro::outln; use super::Dependency; @@ -24,7 +24,45 @@ Please manually delete it from: {}", ); } - fs::remove_dir_all(&dir).await?; + let files = if self.is_flavor { + &["flavor.toml", "tmtheme.xml", "README.md", "preview.png", "LICENSE", "LICENSE-tmtheme"][..] + } else { + &["main.lua", "README.md", "LICENSE"][..] + }; + for p in files.iter().map(|&f| dir.join(f)) { + ok_or_not_found(fs::remove_file(&p).await) + .with_context(|| format!("failed to delete `{}`", p.display()))?; + } + + self.delete_assets().await?; + if ok_or_not_found(fs::remove_dir(&dir).await).is_ok() { + outln!("Done!")?; + } else { + outln!( + "Done! +For safety, user data has been preserved, please manually delete them within: {}", + dir.display() + )?; + } + + Ok(()) + } + + pub(super) async fn delete_assets(&self) -> Result<()> { + let assets = self.target().join("assets"); + match fs::read_dir(&assets).await { + Ok(mut it) => { + while let Some(entry) = it.next_entry().await? { + fs::remove_file(entry.path()) + .await + .with_context(|| format!("failed to remove `{}`", entry.path().display()))?; + } + } + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => Err(e).context(format!("failed to read `{}`", assets.display()))?, + }; + + remove_dir_clean(&assets).await; Ok(()) } } diff --git a/yazi-cli/src/package/deploy.rs b/yazi-cli/src/package/deploy.rs index ff682d9b..8663af52 100644 --- a/yazi-cli/src/package/deploy.rs +++ b/yazi-cli/src/package/deploy.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use anyhow::{Context, Result, bail}; use tokio::fs; -use yazi_fs::{copy_and_seal, maybe_exists, remove_dir_clean}; +use yazi_fs::{copy_and_seal, maybe_exists}; use yazi_macro::outln; use super::Dependency; @@ -49,6 +49,7 @@ Please manually delete it from `{}` and re-run the command.", .with_context(|| format!("failed to copy `{}` to `{}`", from.display(), to.display()))?; } + self.delete_assets().await?; Self::deploy_assets(from.join("assets"), to.join("assets")).await?; outln!("Done!")?; @@ -56,21 +57,6 @@ Please manually delete it from `{}` and re-run the command.", } async fn deploy_assets(from: PathBuf, to: PathBuf) -> Result<()> { - use std::io::ErrorKind::NotFound; - - match fs::read_dir(&to).await { - Ok(mut it) => { - while let Some(entry) = it.next_entry().await? { - fs::remove_file(entry.path()) - .await - .with_context(|| format!("failed to remove `{}`", entry.path().display()))?; - } - } - Err(e) if e.kind() == NotFound => {} - Err(e) => Err(e).context(format!("failed to read `{}`", to.display()))?, - }; - - remove_dir_clean(&to).await; match fs::read_dir(&from).await { Ok(mut it) => { fs::create_dir_all(&to).await?; @@ -81,10 +67,9 @@ Please manually delete it from `{}` and re-run the command.", })?; } } - Err(e) if e.kind() == NotFound => {} + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} Err(e) => Err(e).context(format!("failed to read `{}`", from.display()))?, } - Ok(()) } }