Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): allow setting commands configuration in the site configuration file #98

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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
}

Expand All @@ -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()
Expand Down
24 changes: 24 additions & 0 deletions src/config/commands.rs
Original file line number Diff line number Diff line change
@@ -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 }
9 changes: 8 additions & 1 deletion src/config.rs → src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -18,14 +21,18 @@ 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,
pub language: String,
pub title: String,
pub author: String,
#[serde(default)]
pub build: Option<BuildConfig>,
#[serde(default)]
pub serve: Option<ServeConfig>,
#[serde(default)]
pub content_schema: Option<ContentSchema>,
pub highlighter: Option<SiteConfigHighlighter>,
pub rss: Option<SiteConfigRss>,
Expand Down
Loading