Skip to content

Commit

Permalink
feat: ✨ added build artifact purging to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Sep 7, 2021
1 parent d79f029 commit ef0cf76
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/perseus-cli/src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use perseus_cli::errors::*;
use perseus_cli::{build, check_env, delete_bad_dir, help, prepare, serve, PERSEUS_VERSION};
use perseus_cli::{build, check_env, delete_artifacts, delete_bad_dir, help, prepare, serve, PERSEUS_VERSION};
use std::env;
use std::io::Write;
use std::path::PathBuf;
Expand Down Expand Up @@ -71,11 +71,15 @@ fn core(dir: PathBuf) -> Result<i32> {
if prog_args[0] == "build" {
// Set up the '.perseus/' directory if needed
prepare(dir.clone())?;
// Delete old build artifacts
delete_artifacts(dir.clone())?;
let exit_code = build(dir, &prog_args)?;
Ok(exit_code)
} else if prog_args[0] == "serve" {
// Set up the '.perseus/' directory if needed
prepare(dir.clone())?;
// Delete old build artifacts
delete_artifacts(dir.clone())?;
let exit_code = serve(dir, &prog_args)?;
Ok(exit_code)
} else if prog_args[0] == "prep" {
Expand Down
6 changes: 5 additions & 1 deletion packages/perseus-cli/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ error_chain! {
RemoveBadDirFailed(target: Option<String>, err: String) {
description("removing corrupted '.perseus/' directory failed")
display("Couldn't remove '.perseus/' directory at '{:?}'. Please remove the '.perseus/' directory manually (particularly if you didn't intentionally run the 'clean' command, that means the directory has been corrupted). Error was: '{}'.", target, err)

}
/// For when executing a system command after preparation failed. This shouldn't cause a directory deletion.
CmdExecFailed(cmd: String, err: String) {
Expand All @@ -69,6 +68,11 @@ error_chain! {
description("port in PORT environment variable couldn't be parsed as number")
display("Couldn't parse 'PORT' environment variable as a number, please check that you've provided the correct value. Error was: '{}'.", err)
}
/// For when build artifacts either couldn't be removed or the directory couldn't be recreated.
RemoveArtifactsFailed(target: Option<String>, err: String) {
description("reconstituting build artifacts failed")
display("Couldn't remove and replace '.perseus/dist/static/' directory at '{:?}'. Please try again or run 'perseus clean' if the error persists. Error was: '{}'.", target, err)
}
}
}

Expand Down
26 changes: 25 additions & 1 deletion packages/perseus-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub use help::help;
pub use prepare::{check_env, prepare};
pub use serve::serve;

/// Deletes a corrupted '.perseus/' directory. This qwill be called on certain error types that would leave the user with a half-finished
/// Deletes a corrupted '.perseus/' directory. This will be called on certain error types that would leave the user with a half-finished
/// product, which is better to delete for safety and sanity.
pub fn delete_bad_dir(dir: PathBuf) -> Result<()> {
let mut target = dir;
Expand All @@ -63,3 +63,27 @@ pub fn delete_bad_dir(dir: PathBuf) -> Result<()> {
}
Ok(())
}

/// Deletes build artifacts in `.perseus/dist/static` and replaces the directory.
pub fn delete_artifacts(dir: PathBuf) -> Result<()> {
let mut target = dir;
target.extend([".perseus", "dist", "static"]);
// We'll only delete the directory if it exists, otherwise we're fine
if target.exists() {
if let Err(err) = fs::remove_dir_all(&target) {
bail!(ErrorKind::RemoveArtifactsFailed(
target.to_str().map(|s| s.to_string()),
err.to_string()
))
}
}
// No matter what, it's gone now, so recreate it
if let Err(err) = fs::create_dir(&target) {
bail!(ErrorKind::RemoveArtifactsFailed(
target.to_str().map(|s| s.to_string()),
err.to_string()
))
}

Ok(())
}

0 comments on commit ef0cf76

Please sign in to comment.