Skip to content

Commit

Permalink
Add Checking and Force Flag for Directory in Serve Command (getzola#2265
Browse files Browse the repository at this point in the history
)

* Introduce option to force directory when running the serve command

* Update documentation about the force flag on the serve command

* Resolve cargo fmt issue

* Reword new serve flag documentation
  • Loading branch information
SquirrelHub authored and Erwin Vrolijk committed Sep 30, 2023
1 parent 3ff0bd2 commit 77dcdf7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/content/documentation/getting-started/cli-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ pub enum Command {
#[clap(short = 'o', long)]
output_dir: Option<PathBuf>,

/// 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,
Expand Down
12 changes: 11 additions & 1 deletion src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -391,6 +399,7 @@ pub fn serve(
interface,
interface_port,
output_dir,
force,
base_url,
config_file,
include_drafts,
Expand Down Expand Up @@ -599,6 +608,7 @@ pub fn serve(
interface,
interface_port,
output_dir,
force,
base_url,
config_file,
include_drafts,
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ fn main() {
interface,
mut port,
output_dir,
force,
base_url,
drafts,
open,
Expand All @@ -104,6 +105,7 @@ fn main() {
&interface,
port,
output_dir.as_deref(),
force,
&base_url,
&config_file,
open,
Expand Down

0 comments on commit 77dcdf7

Please sign in to comment.