Skip to content

Commit

Permalink
Add optional arg to serve for extra paths to watch for changes (#2745)
Browse files Browse the repository at this point in the history
* Add optional arg to serve for extra paths to watch for changes

* Address feedback
  • Loading branch information
rparrett authored Dec 26, 2024
1 parent a586d95 commit 0a43f61
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ pub enum Command {
/// Default append port to the base url.
#[clap(long)]
no_port_append: bool,

/// Extra path to watch for changes, relative to the project root.
#[clap(long)]
extra_watch_path: Vec<String>,
},

/// Try to build the project without rendering it. Checks links
Expand Down
27 changes: 25 additions & 2 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ pub fn serve(
fast_rebuild: bool,
no_port_append: bool,
utc_offset: UtcOffset,
extra_watch_paths: Vec<String>,
) -> Result<()> {
let start = Instant::now();
let (mut site, bind_address, constructed_base_url) = create_new_site(
Expand Down Expand Up @@ -462,8 +463,8 @@ pub fn serve(
// An array of (path, WatchMode, RecursiveMode) where the path is watched for changes,
// the WatchMode value indicates whether this path must exist for zola serve to operate,
// and the RecursiveMode value indicates whether to watch nested directories.
let watch_this = vec![
// The first entry is ultimtely to watch config.toml in a more robust manner on Linux when
let mut watch_this = vec![
// The first entry is ultimately to watch config.toml in a more robust manner on Linux when
// the file changes by way of a caching strategy used by editors such as vim.
// https://github.com/getzola/zola/issues/2266
(root_dir_str, WatchMode::Required, RecursiveMode::NonRecursive),
Expand All @@ -473,6 +474,11 @@ pub fn serve(
("templates", WatchMode::Optional, RecursiveMode::Recursive),
("themes", WatchMode::Condition(site.config.theme.is_some()), RecursiveMode::Recursive),
];
watch_this.extend(
extra_watch_paths
.iter()
.map(|path| (path.as_str(), WatchMode::Required, RecursiveMode::Recursive)),
);

// Setup watchers
let (tx, rx) = channel();
Expand Down Expand Up @@ -810,6 +816,23 @@ pub fn serve(
site = s;
}
}
ChangeKind::ExtraPath => {
let full_paths: Vec<&PathBuf> =
change_group.iter().map(|(_, p, _)| p).collect();
let combined_paths = full_paths
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<String>>()
.join(", ");
console::info(&format!(
"-> {combined_paths} changed. Recreating whole site."
));

// We can't know exactly what to update when a user provides the path.
if let Some(s) = recreate_site() {
site = s;
}
}
};
messages::report_elapsed_time(start);
}
Expand Down
4 changes: 3 additions & 1 deletion src/fs_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub enum ChangeKind {
StaticFiles,
Sass,
Config,
/// A change in one of the extra paths to watch provided by the user.
ExtraPath,
}

/// This enum abstracts over the fine-grained group of enums in `notify`.
Expand Down Expand Up @@ -160,7 +162,7 @@ fn detect_change_kind(pwd: &Path, path: &Path, config_path: &Path) -> (ChangeKin
} else if path == config_path {
ChangeKind::Config
} else {
unreachable!("Got a change in an unexpected path: {}", partial_path.display());
ChangeKind::ExtraPath
};

(change_kind, partial_path)
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fn main() {
open,
fast,
no_port_append,
extra_watch_path,
} => {
if port != 1111 && !port_is_available(port) {
console::error("The requested port is not available");
Expand Down Expand Up @@ -114,6 +115,7 @@ fn main() {
fast,
no_port_append,
UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC),
extra_watch_path,
) {
messages::unravel_errors("Failed to serve the site", &e);
std::process::exit(1);
Expand Down

0 comments on commit 0a43f61

Please sign in to comment.