Fix --watch-only to actually rebuild the site

This commit is contained in:
Robert O'Callahan 2019-01-25 13:47:30 +13:00
parent 1e2dd9ce03
commit 986c49daf1

View file

@ -90,9 +90,10 @@ fn livereload_handler(_: &HttpRequest) -> &'static str {
LIVE_RELOAD
}
fn rebuild_done_handling(broadcaster: &Sender, res: Result<()>, reload_path: &str) {
fn rebuild_done_handling(broadcaster: &Option<Sender>, res: Result<()>, reload_path: &str) {
match res {
Ok(_) => {
if let Some(broadcaster) = broadcaster.as_ref() {
broadcaster
.send(format!(
r#"
@ -108,6 +109,7 @@ fn rebuild_done_handling(broadcaster: &Sender, res: Result<()>, reload_path: &st
))
.unwrap();
}
}
Err(e) => console::unravel_errors("Failed to build the site", &e),
}
}
@ -293,14 +295,12 @@ pub fn serve(
format!("-> Template changed {}", path.display())
};
console::info(&msg);
if let Some(ref broadcaster) = broadcaster {
// Force refresh
rebuild_done_handling(
broadcaster,
&broadcaster,
rebuild::after_template_change(site, &path),
"/x.js",
);
}
};
let reload_sass = |site: &Site, path: &Path, partial_path: &Path| {
@ -310,13 +310,11 @@ pub fn serve(
format!("-> Sass file changed {}", path.display())
};
console::info(&msg);
if let Some(ref broadcaster) = broadcaster {
rebuild_done_handling(
&broadcaster,
site.compile_sass(&site.base_path),
&partial_path.to_string_lossy(),
);
}
};
let copy_static = |site: &Site, path: &Path, partial_path: &Path| {
@ -332,21 +330,19 @@ pub fn serve(
};
console::info(&msg);
if let Some(ref broadcaster) = broadcaster {
if path.is_dir() {
rebuild_done_handling(
broadcaster,
&broadcaster,
site.copy_static_directories(),
&path.to_string_lossy(),
);
} else {
rebuild_done_handling(
broadcaster,
&broadcaster,
copy_file(&path, &site.output_path, &site.static_path),
&partial_path.to_string_lossy(),
);
}
}
};
loop {
@ -373,15 +369,13 @@ pub fn serve(
match change_kind {
ChangeKind::Content => {
console::info(&format!("-> Content renamed {}", path.display()));
if let Some(ref broadcaster) = broadcaster {
// Force refresh
rebuild_done_handling(
broadcaster,
&broadcaster,
rebuild::after_content_rename(&mut site, &old_path, &path),
"/x.js",
);
}
}
ChangeKind::Templates => reload_templates(&mut site, &path),
ChangeKind::StaticFiles => copy_static(&site, &path, &partial_path),
ChangeKind::Sass => reload_sass(&site, &path, &partial_path),
@ -414,15 +408,13 @@ pub fn serve(
match detect_change_kind(&pwd, &path) {
(ChangeKind::Content, _) => {
console::info(&format!("-> Content changed {}", path.display()));
if let Some(ref broadcaster) = broadcaster {
// Force refresh
rebuild_done_handling(
broadcaster,
&broadcaster,
rebuild::after_content_change(&mut site, &path),
"/x.js",
);
}
}
(ChangeKind::Templates, _) => reload_templates(&mut site, &path),
(ChangeKind::StaticFiles, p) => copy_static(&site, &path, &p),
(ChangeKind::Sass, p) => reload_sass(&site, &path, &p),