diff --git a/src/cli.rs b/src/cli.rs index 56dbd4a..added9f 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -121,9 +121,13 @@ enum Commands { short = 'm', long, default_value_t = true, + overrides_with = "_no_minify", help = "Minify the produced assets" )] minify: bool, + + #[arg(long = "no-minify")] + _no_minify: bool, }, } @@ -152,7 +156,7 @@ pub async fn start() -> Result<()> { host, open, } => check_and_serve(*port, !_no_drafts, *open, *host).await?, - Commands::Build { minify } => build_site(*minify).await?, + Commands::Build { minify: _, _no_minify } => build_site(!_no_minify).await?, Commands::New { kind, name, open } => { new_asset(kind.as_ref(), name.as_ref(), *open).await? } // _ => bail!("Unsupported command"), @@ -185,6 +189,20 @@ async fn init_site(name: Option<&String>, prompt: bool) -> Result<()> { /// # Returns: /// A `Result<()>` indicating success or error. async fn build_site(minify: bool) -> Result<()> { + let build_config = match crate::fs::find_config_file().await? { + Some(config_path) => { + let config_content = tokio::fs::read_to_string(config_path).await?; + toml::from_str(&config_content)? + } + None => crate::config::SiteConfig::default(), + } + .build + .unwrap_or_default(); + + // Merge CLI and config values + // CLI options have higher priority than config + // config has higher priority than defaults + let minify = minify || build_config.minify; cmd::build(minify).await } @@ -200,6 +218,30 @@ async fn build_site(minify: bool) -> Result<()> { /// A `Result<()>` indicating success or error. On error, the context message /// will provide information on why the development server could not be initialized. async fn check_and_serve(port: u16, drafts: bool, open: bool, host: bool) -> Result<()> { + let serve_config = match crate::fs::find_config_file().await? { + Some(config_path) => { + let config_content = tokio::fs::read_to_string(config_path).await?; + toml::from_str(&config_content)? + } + None => crate::config::SiteConfig::default(), + } + .serve + .unwrap_or_default(); + + // Merge CLI and config values + // CLI options have higher priority than config + // config has higher priority than defaults + let port = if port != 3030 { + port + } else if serve_config.port == 0 { + 3030 + } else { + serve_config.port + }; + let drafts = drafts || serve_config.drafts; + let host = host || serve_config.host; + let open = open || serve_config.open; + if !net::is_port_available(port) { let port_msg = if port == 3030 { "default Norgolith port (3030)".to_string() diff --git a/src/config/commands.rs b/src/config/commands.rs new file mode 100644 index 0000000..5601524 --- /dev/null +++ b/src/config/commands.rs @@ -0,0 +1,24 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Default, Debug, Clone, Deserialize, Serialize)] +pub struct BuildConfig { + #[serde(default = "default_minify")] + pub minify: bool, +} + +fn default_minify() -> bool { true } + +#[derive(Default, Debug, Clone, Deserialize, Serialize)] +pub struct ServeConfig { + #[serde(default = "default_port")] + pub port: u16, + #[serde(default = "default_drafts")] + pub drafts: bool, + #[serde(default)] + pub host: bool, + #[serde(default)] + pub open: bool, +} + +fn default_port() -> u16 { 3030 } +fn default_drafts() -> bool { true } diff --git a/src/config.rs b/src/config/mod.rs similarity index 77% rename from src/config.rs rename to src/config/mod.rs index 5f3713f..1d936f0 100644 --- a/src/config.rs +++ b/src/config/mod.rs @@ -3,6 +3,9 @@ use std::collections::HashMap; use serde::{Deserialize, Serialize}; use crate::schema::ContentSchema; +use commands::{BuildConfig, ServeConfig}; + +pub mod commands; #[derive(Debug, Clone, Deserialize, Serialize)] pub struct SiteConfigHighlighter { @@ -18,7 +21,7 @@ pub struct SiteConfigRss { pub image: String, } -#[derive(Debug, Clone, Deserialize, Serialize)] +#[derive(Default, Debug, Clone, Deserialize, Serialize)] pub struct SiteConfig { #[serde(rename = "rootUrl")] pub root_url: String, @@ -26,6 +29,10 @@ pub struct SiteConfig { pub title: String, pub author: String, #[serde(default)] + pub build: Option, + #[serde(default)] + pub serve: Option, + #[serde(default)] pub content_schema: Option, pub highlighter: Option, pub rss: Option,