use std::{path::Path, time::Instant};
use color_eyre::eyre::{Result, WrapErr};
use typstify_core::Config;
use typstify_generator::Builder;
use super::check::quick_validate;
pub fn run(
config_path: &Path,
output: &Path,
drafts: bool,
host: Option<&str>,
base_path: Option<&str>,
) -> Result<()> {
let start = Instant::now();
tracing::info!(
?config_path,
?output,
drafts,
?host,
?base_path,
"Starting build"
);
let mut config = Config::load_with_env(config_path).wrap_err("Failed to load configuration")?;
let warnings = quick_validate(&config);
if !warnings.is_empty() {
println!();
println!(" Warnings:");
for warn in &warnings {
println!(" ⚠ {warn}");
}
println!();
}
config.build.output_dir = output.to_string_lossy().to_string();
config.build.drafts = drafts;
if let Some(h) = host {
tracing::info!(host = h, "Overriding site host from CLI");
config.site.host = h.to_string();
}
if let Some(bp) = base_path {
tracing::info!(base_path = bp, "Overriding site base_path from CLI");
config.site.base_path = bp.to_string();
}
tracing::debug!(?config, "Loaded configuration");
let content_dir = Path::new("content");
let mut builder = Builder::new(config, content_dir, output);
let static_dir = Path::new("static");
if static_dir.exists() && static_dir.is_dir() {
tracing::info!("Found static directory, will copy to output");
builder = builder.with_static_dir(static_dir);
}
let stats = builder.build().wrap_err("Build failed")?;
let duration = start.elapsed();
println!();
println!(" Build completed successfully!");
println!();
println!(" Pages: {}", stats.pages);
println!(" Taxonomies: {}", stats.taxonomy_pages);
println!(" Auto Pages: {}", stats.auto_pages);
println!(" Redirects: {}", stats.redirects);
println!(" Assets: {}", stats.assets);
println!();
println!(" Duration: {:.2}s", duration.as_secs_f64());
println!(" Output: {}", output.display());
println!();
tracing::info!(?stats, ?duration, "Build completed successfully");
Ok(())
}