1use {
2 crate::{error::CliResult, style},
3 std::{fs, path::Path, process::Command},
4};
5
6pub fn run(all: bool) -> CliResult {
7 let dirs = [
8 "target/deploy",
9 "target/profile",
10 "target/idl",
11 "target/client",
12 ];
13
14 let removed: Vec<&str> = dirs
15 .iter()
16 .filter(|d| Path::new(d).exists())
17 .copied()
18 .collect();
19
20 if removed.is_empty() && !all {
21 println!(" {}", style::dim("nothing to clean"));
22 return Ok(());
23 }
24
25 for dir in &removed {
26 fs::remove_dir_all(Path::new(dir))?;
27 }
28
29 if all {
30 let output = Command::new("cargo")
31 .arg("clean")
32 .output()
33 .map_err(anyhow::Error::from)?;
34 if !output.status.success() {
35 let stderr = String::from_utf8_lossy(&output.stderr);
36 eprintln!(
37 " {}",
38 style::fail(&format!("cargo clean failed: {}", stderr.trim()))
39 );
40 std::process::exit(1);
41 }
42 }
43
44 println!(" {}", style::success("clean"));
45 Ok(())
46}