Add Checking and Force Flag for Directory in Serve Command (#2265)

* 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
This commit is contained in:
SquirrelHub 2023-08-17 08:26:34 -05:00 committed by Vincent Prouillet
parent 841b3dfc92
commit 8b2b066e64
4 changed files with 19 additions and 1 deletions

View file

@ -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

View file

@ -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,

View file

@ -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,

View file

@ -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,