Skip to content

Commit ef0cf76

Browse files
committed
feat: ✨ added build artifact purging to cli
1 parent d79f029 commit ef0cf76

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

packages/perseus-cli/src/bin/main.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use perseus_cli::errors::*;
2-
use perseus_cli::{build, check_env, delete_bad_dir, help, prepare, serve, PERSEUS_VERSION};
2+
use perseus_cli::{build, check_env, delete_artifacts, delete_bad_dir, help, prepare, serve, PERSEUS_VERSION};
33
use std::env;
44
use std::io::Write;
55
use std::path::PathBuf;
@@ -71,11 +71,15 @@ fn core(dir: PathBuf) -> Result<i32> {
7171
if prog_args[0] == "build" {
7272
// Set up the '.perseus/' directory if needed
7373
prepare(dir.clone())?;
74+
// Delete old build artifacts
75+
delete_artifacts(dir.clone())?;
7476
let exit_code = build(dir, &prog_args)?;
7577
Ok(exit_code)
7678
} else if prog_args[0] == "serve" {
7779
// Set up the '.perseus/' directory if needed
7880
prepare(dir.clone())?;
81+
// Delete old build artifacts
82+
delete_artifacts(dir.clone())?;
7983
let exit_code = serve(dir, &prog_args)?;
8084
Ok(exit_code)
8185
} else if prog_args[0] == "prep" {

packages/perseus-cli/src/errors.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ error_chain! {
4242
RemoveBadDirFailed(target: Option<String>, err: String) {
4343
description("removing corrupted '.perseus/' directory failed")
4444
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)
45-
4645
}
4746
/// For when executing a system command after preparation failed. This shouldn't cause a directory deletion.
4847
CmdExecFailed(cmd: String, err: String) {
@@ -69,6 +68,11 @@ error_chain! {
6968
description("port in PORT environment variable couldn't be parsed as number")
7069
display("Couldn't parse 'PORT' environment variable as a number, please check that you've provided the correct value. Error was: '{}'.", err)
7170
}
71+
/// For when build artifacts either couldn't be removed or the directory couldn't be recreated.
72+
RemoveArtifactsFailed(target: Option<String>, err: String) {
73+
description("reconstituting build artifacts failed")
74+
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)
75+
}
7276
}
7377
}
7478

packages/perseus-cli/src/lib.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub use help::help;
4747
pub use prepare::{check_env, prepare};
4848
pub use serve::serve;
4949

50-
/// Deletes a corrupted '.perseus/' directory. This qwill be called on certain error types that would leave the user with a half-finished
50+
/// Deletes a corrupted '.perseus/' directory. This will be called on certain error types that would leave the user with a half-finished
5151
/// product, which is better to delete for safety and sanity.
5252
pub fn delete_bad_dir(dir: PathBuf) -> Result<()> {
5353
let mut target = dir;
@@ -63,3 +63,27 @@ pub fn delete_bad_dir(dir: PathBuf) -> Result<()> {
6363
}
6464
Ok(())
6565
}
66+
67+
/// Deletes build artifacts in `.perseus/dist/static` and replaces the directory.
68+
pub fn delete_artifacts(dir: PathBuf) -> Result<()> {
69+
let mut target = dir;
70+
target.extend([".perseus", "dist", "static"]);
71+
// We'll only delete the directory if it exists, otherwise we're fine
72+
if target.exists() {
73+
if let Err(err) = fs::remove_dir_all(&target) {
74+
bail!(ErrorKind::RemoveArtifactsFailed(
75+
target.to_str().map(|s| s.to_string()),
76+
err.to_string()
77+
))
78+
}
79+
}
80+
// No matter what, it's gone now, so recreate it
81+
if let Err(err) = fs::create_dir(&target) {
82+
bail!(ErrorKind::RemoveArtifactsFailed(
83+
target.to_str().map(|s| s.to_string()),
84+
err.to_string()
85+
))
86+
}
87+
88+
Ok(())
89+
}

0 commit comments

Comments
 (0)