Get the output directory and clear it.

This commit is contained in:
Tom Alexander 2023-10-22 14:40:59 -04:00
parent 07e4209048
commit a9fbb4cd63
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 26 additions and 0 deletions

View File

@ -1,3 +1,5 @@
use std::path::PathBuf;
use crate::blog_post::BlogPost;
use crate::cli::parameters::BuildArgs;
use crate::config::Config;
@ -7,6 +9,7 @@ use walkdir::WalkDir;
pub(crate) async fn build_site(args: BuildArgs) -> Result<(), CustomError> {
let config = Config::load_from_file(args.config).await?;
let root_directory = config.get_root_directory().to_owned();
let output_directory = get_output_directory(&config).await?;
let post_directories = WalkDir::new(config.get_posts_directory())
.into_iter()
.filter(|e| match e {
@ -26,3 +29,22 @@ pub(crate) async fn build_site(args: BuildArgs) -> Result<(), CustomError> {
println!("{:?}", blog_posts);
Ok(())
}
/// Delete everything inside the output directory and return the path to that directory.
async fn get_output_directory(config: &Config) -> Result<PathBuf, CustomError> {
let output_directory = config.get_output_directory();
if !output_directory.exists() {
tokio::fs::create_dir(&output_directory).await?;
} else {
let mut existing_entries = tokio::fs::read_dir(&output_directory).await?;
while let Some(entry) = existing_entries.next_entry().await? {
let file_type = entry.file_type().await?;
if file_type.is_dir() {
tokio::fs::remove_dir_all(entry.path()).await?;
} else {
tokio::fs::remove_file(entry.path()).await?;
}
}
}
Ok(output_directory)
}

View File

@ -55,4 +55,8 @@ impl Config {
pub(crate) fn get_posts_directory(&self) -> PathBuf {
self.get_root_directory().join("posts")
}
pub(crate) fn get_output_directory(&self) -> PathBuf {
self.get_root_directory().join("output")
}
}