diff --git a/docs/content/documentation/getting-started/cli-usage.md b/docs/content/documentation/getting-started/cli-usage.md index 10275037f3..b8d98637ae 100644 --- a/docs/content/documentation/getting-started/cli-usage.md +++ b/docs/content/documentation/getting-started/cli-usage.md @@ -81,6 +81,8 @@ web browser. Before starting, Zola will delete the output directory (by default `public` in project root) to start from a clean slate. +If you are specifying the directory but are also using the `output-dir` flag, Zola will not use the specified directory if it already exists unless the --force flag is used. + ```bash $ zola serve $ zola serve --port 2000 diff --git a/src/cli.rs b/src/cli.rs index 8155fefad8..b55dc18a5e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -65,6 +65,10 @@ pub enum Command { #[clap(short = 'o', long)] output_dir: Option, + /// Force use of the directory for serving the site even if output directory is non-empty + #[clap(long)] + force: bool, + /// Changes the base_url #[clap(short = 'u', long, default_value = "127.0.0.1")] base_url: String, diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index f5c1d2a303..feec978b20 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -47,7 +47,7 @@ use libs::serde_json; use notify::{watcher, RecursiveMode, Watcher}; use ws::{Message, Sender, WebSocket}; -use errors::{anyhow, Context, Result}; +use errors::{anyhow, Context, Error, Result}; use pathdiff::diff_paths; use site::sass::compile_sass; use site::{Site, SITE_CONTENT}; @@ -324,6 +324,7 @@ fn create_new_site( interface: &str, interface_port: u16, output_dir: Option<&Path>, + force: bool, base_url: &str, config_file: &Path, include_drafts: bool, @@ -354,6 +355,12 @@ fn create_new_site( site.enable_serve_mode(); site.set_base_url(base_url); if let Some(output_dir) = output_dir { + if !force && output_dir.exists() { + return Err(Error::msg(format!( + "Directory '{}' already exists. Use --force to overwrite.", + output_dir.display(), + ))); + } site.set_output_path(output_dir); } if include_drafts { @@ -377,6 +384,7 @@ pub fn serve( interface: &str, interface_port: u16, output_dir: Option<&Path>, + force: bool, base_url: &str, config_file: &Path, open: bool, @@ -391,6 +399,7 @@ pub fn serve( interface, interface_port, output_dir, + force, base_url, config_file, include_drafts, @@ -599,6 +608,7 @@ pub fn serve( interface, interface_port, output_dir, + force, base_url, config_file, include_drafts, diff --git a/src/main.rs b/src/main.rs index a65af39026..59bc80df5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,6 +79,7 @@ fn main() { interface, mut port, output_dir, + force, base_url, drafts, open, @@ -104,6 +105,7 @@ fn main() { &interface, port, output_dir.as_deref(), + force, &base_url, &config_file, open,