use crate::error::HylixResult;
use crate::logging::{log_info, log_success};
use crate::process::execute_and_check;
use crate::validation::{validate_project, ValidationLevel};
use std::path::Path;
pub async fn execute() -> HylixResult<()> {
log_info("Cleaning build artifacts...");
validate_project(ValidationLevel::Clean)?;
if Path::new("contracts").exists() {
log_info("Cleaning contracts...");
clean_contracts().await?;
}
if Path::new("server").exists() {
log_info("Cleaning server...");
clean_server().await?;
}
if Path::new("front").exists() {
log_info("Cleaning frontend...");
clean_frontend().await?;
}
if Path::new("tests").exists() {
log_info("Cleaning test artifacts...");
clean_test_artifacts().await?;
}
log_success("Clean completed successfully!");
Ok(())
}
async fn clean_contracts() -> HylixResult<()> {
execute_and_check(
"cargo",
&["clean"],
Some("contracts"),
"Failed to clean contracts",
)
.await?;
clean_contract_specific_artifacts().await?;
Ok(())
}
async fn clean_server() -> HylixResult<()> {
execute_and_check(
"cargo",
&["clean"],
Some("server"),
"Failed to clean server",
)
.await?;
clean_server_specific_artifacts().await?;
Ok(())
}
async fn clean_frontend() -> HylixResult<()> {
if which::which("bun").is_ok()
&& execute_and_check(
"bun",
&["run", "clean"],
Some("front"),
"Failed to clean frontend",
)
.await
.is_ok()
{
return Ok(());
}
let build_dirs = ["dist", "build", "out", ".next", "node_modules/.cache"];
for dir in &build_dirs {
let path = Path::new("front").join(dir);
if path.exists() {
std::fs::remove_dir_all(&path)?;
log_info(&format!("Removed {}", path.display()));
}
}
Ok(())
}
async fn clean_test_artifacts() -> HylixResult<()> {
let test_dirs = ["test-results", "coverage", ".nyc_output"];
for dir in &test_dirs {
let path = Path::new(dir);
if path.exists() {
std::fs::remove_dir_all(path)?;
log_info(&format!("Removed {}", path.display()));
}
}
Ok(())
}
async fn clean_contract_specific_artifacts() -> HylixResult<()> {
let sp1_dirs = ["target/sp1", "sp1-artifacts"];
for dir in &sp1_dirs {
let path = Path::new("contracts").join(dir);
if path.exists() {
std::fs::remove_dir_all(&path)?;
log_info(&format!("Removed SP1 artifacts: {}", path.display()));
}
}
let risc0_dirs = ["target/risc0", "risc0-artifacts"];
for dir in &risc0_dirs {
let path = Path::new("contracts").join(dir);
if path.exists() {
std::fs::remove_dir_all(&path)?;
log_info(&format!("Removed Risc0 artifacts: {}", path.display()));
}
}
let proof_dirs = ["proofs", "artifacts"];
for dir in &proof_dirs {
let path = Path::new("contracts").join(dir);
if path.exists() {
std::fs::remove_dir_all(&path)?;
log_info(&format!("Removed proof artifacts: {}", path.display()));
}
}
Ok(())
}
async fn clean_server_specific_artifacts() -> HylixResult<()> {
let log_dirs = ["logs", "log"];
for dir in &log_dirs {
let path = Path::new("server").join(dir);
if path.exists() {
std::fs::remove_dir_all(&path)?;
log_info(&format!("Removed server logs: {}", path.display()));
}
}
let data_dirs = ["data", "db", "cache"];
for dir in &data_dirs {
let path = Path::new("server").join(dir);
if path.exists() {
std::fs::remove_dir_all(&path)?;
log_info(&format!("Removed server data: {}", path.display()));
}
}
Ok(())
}