Skip to content

Commit

Permalink
feat(cli): add clean command to stctl (#117)
Browse files Browse the repository at this point in the history
* feat(cli): add clean command to stctl

* chore(cli): add version

* fix(cli): count time cleaning build and data dir as well
  • Loading branch information
futursolo authored Jul 16, 2023
1 parent 30541ac commit ee45664
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/stctl/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ pub(crate) enum CliCommand {
Serve(ServeCommand),
/// Build the server and client for final distribution.
Build(BuildCommand),
/// Cleans the artifact generated by stctl, cargo and trunk.
Clean,
}

#[derive(Parser, Debug)]
#[command(author, version, about = "Command line tool for stellation.", long_about = None)]
pub(crate) struct Cli {
/// The path to the manifest file.
///
Expand Down
56 changes: 56 additions & 0 deletions crates/stctl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl Stctl {
Profile::new_debug()
}
}
CliCommand::Clean => Profile::new_debug(),
};

let env_name = match cli.command {
Expand Down Expand Up @@ -220,6 +221,9 @@ impl Stctl {
let frontend_data_dir = self.frontend_data_dir().await?;
frontend_data_dir.join("serve-builds").join(random_str()?)
}
CliCommand::Clean => {
bail!("clean command never access build dir!")
}
};

fs::create_dir_all(&frontend_build_dir)
Expand All @@ -239,6 +243,9 @@ impl Stctl {
let frontend_data_dir = self.backend_data_dir().await?;
frontend_data_dir.join("serve-builds").join(random_str()?)
}
CliCommand::Clean => {
bail!("clean command never access build dir!")
}
};

fs::create_dir_all(&frontend_build_dir)
Expand Down Expand Up @@ -631,6 +638,52 @@ impl Stctl {
Ok(())
}

async fn run_clean(&self) -> Result<()> {
use tokio::process::Command;

let workspace_dir = self.workspace_dir().await?;
let build_dir = self.build_dir().await?;
let data_dir = self.data_dir().await?;

let envs = self.env_file.load(&workspace_dir);

let start_time = SystemTime::now();

Command::new("cargo")
.arg("clean")
.current_dir(&workspace_dir)
.envs(envs)
.stdin(Stdio::null())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.kill_on_drop(true)
.spawn()?
.wait_with_output()
.await
.context("failed to clean cargo data")?;

fs::remove_dir_all(&build_dir)
.await
.context("failed to clean build dir")?;
fs::remove_dir_all(&data_dir)
.await
.context("failed to clean data dir")?;

let time_taken_in_f64 =
f64::try_from(i32::try_from(start_time.elapsed()?.as_millis())?)? / 1000.0;

eprintln!(
"{}",
style(format!(
"Build artifact and temporary files cleared in {time_taken_in_f64:.2}s!"
))
.green()
.bold()
);

Ok(())
}

async fn run(&self) -> Result<()> {
match self.cli.command {
CliCommand::Serve(ref m) => {
Expand All @@ -639,6 +692,9 @@ impl Stctl {
CliCommand::Build(ref m) => {
self.run_build(m).await?;
}
CliCommand::Clean => {
self.run_clean().await?;
}
}

Ok(())
Expand Down

0 comments on commit ee45664

Please sign in to comment.