use std::path::PathBuf; use crate::blog_post::convert_blog_post_to_render_context; use crate::blog_post::BlogPost; use crate::cli::parameters::BuildArgs; use crate::config::Config; use crate::error::CustomError; pub(crate) async fn build_site(args: BuildArgs) -> Result<(), CustomError> { let config = Config::load_from_file(args.config).await?; let blog_posts = load_blog_posts(&config).await?; println!("{:?}", blog_posts); for blog_post in &blog_posts { let render_context = convert_blog_post_to_render_context(blog_post); println!("{}", serde_json::to_string(&render_context)?); } Ok(()) } /// Delete everything inside the output directory and return the path to that directory. async fn get_output_directory(config: &Config) -> Result { 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) } async fn get_post_directories(config: &Config) -> Result, CustomError> { let mut ret = Vec::new(); let mut entries = tokio::fs::read_dir(config.get_posts_directory()).await?; while let Some(entry) = entries.next_entry().await? { let file_type = entry.file_type().await?; if file_type.is_dir() { ret.push(entry.path()); } } Ok(ret) } async fn load_blog_posts(config: &Config) -> Result, CustomError> { let root_directory = config.get_root_directory().to_owned(); let post_directories = get_post_directories(&config).await?; let load_jobs = post_directories .into_iter() .map(|path| tokio::spawn(BlogPost::load_blog_post(root_directory.clone(), path))); let mut blog_posts = Vec::new(); for job in load_jobs { blog_posts.push(job.await??); } Ok(blog_posts) }