add --watch-only flag

This commit is contained in:
Robert Masen 2018-11-01 17:20:35 -05:00
parent cb3c42078a
commit b7c3d7199d
3 changed files with 86 additions and 65 deletions

View file

@ -62,6 +62,10 @@ pub fn build_cli() -> App<'static, 'static> {
.default_value("127.0.0.1") .default_value("127.0.0.1")
.takes_value(true) .takes_value(true)
.help("Changes the base_url"), .help("Changes the base_url"),
Arg::with_name("watch_only")
.long("watch-only")
.takes_value(false)
.help("Do not start a server, just re-build project on changes")
]), ]),
]) ])
} }

View file

@ -166,6 +166,7 @@ pub fn serve(
output_dir: &str, output_dir: &str,
base_url: &str, base_url: &str,
config_file: &str, config_file: &str,
watch_only: bool,
) -> Result<()> { ) -> Result<()> {
let start = Instant::now(); let start = Instant::now();
let (mut site, address) = create_new_site(interface, port, output_dir, base_url, config_file)?; let (mut site, address) = create_new_site(interface, port, output_dir, base_url, config_file)?;
@ -206,6 +207,8 @@ pub fn serve(
// output path is going to need to be moved later on, so clone it for the // output path is going to need to be moved later on, so clone it for the
// http closure to avoid contention. // http closure to avoid contention.
let static_root = output_path.clone(); let static_root = output_path.clone();
let broadcaster = if !watch_only {
thread::spawn(move || { thread::spawn(move || {
let s = server::new(move || { let s = server::new(move || {
App::new() App::new()
@ -226,7 +229,6 @@ pub fn serve(
println!("Web server is available at http://{}", &address); println!("Web server is available at http://{}", &address);
s.run(); s.run();
}); });
// The websocket for livereload // The websocket for livereload
let ws_server = WebSocket::new(|output: Sender| { let ws_server = WebSocket::new(|output: Sender| {
move |msg: Message| { move |msg: Message| {
@ -249,6 +251,12 @@ pub fn serve(
thread::spawn(move || { thread::spawn(move || {
ws_server.listen(&*ws_address).unwrap(); ws_server.listen(&*ws_address).unwrap();
}); });
Some(broadcaster)
} else {
println!("Watching in watch only mode, no web server will be started");
None
};
let pwd = env::current_dir().unwrap(); let pwd = env::current_dir().unwrap();
@ -297,43 +305,51 @@ pub fn serve(
match detect_change_kind(&pwd, &path) { match detect_change_kind(&pwd, &path) {
(ChangeKind::Content, _) => { (ChangeKind::Content, _) => {
console::info(&format!("-> Content changed {}", path.display())); console::info(&format!("-> Content changed {}", path.display()));
if let Some(ref broadcaster) = broadcaster {
// Force refresh // Force refresh
rebuild_done_handling( rebuild_done_handling(
&broadcaster, broadcaster,
rebuild::after_content_change(&mut site, &path), rebuild::after_content_change(&mut site, &path),
"/x.js", "/x.js",
); );
} }
}
(ChangeKind::Templates, _) => { (ChangeKind::Templates, _) => {
console::info(&format!("-> Template changed {}", path.display())); console::info(&format!("-> Template changed {}", path.display()));
if let Some(ref broadcaster) = broadcaster {
// Force refresh // Force refresh
rebuild_done_handling( rebuild_done_handling(
&broadcaster, broadcaster,
rebuild::after_template_change(&mut site, &path), rebuild::after_template_change(&mut site, &path),
"/x.js", "/x.js",
); );
} }
}
(ChangeKind::StaticFiles, p) => { (ChangeKind::StaticFiles, p) => {
if path.is_file() { if path.is_file() {
console::info(&format!( console::info(&format!(
"-> Static file changes detected {}", "-> Static file changes detected {}",
path.display() path.display()
)); ));
if let Some(ref broadcaster) = broadcaster {
rebuild_done_handling( rebuild_done_handling(
&broadcaster, broadcaster,
copy_file(&path, &site.output_path, &site.static_path), copy_file(&path, &site.output_path, &site.static_path),
&p.to_string_lossy(), &p.to_string_lossy(),
); );
} }
} }
}
(ChangeKind::Sass, p) => { (ChangeKind::Sass, p) => {
console::info(&format!("-> Sass file changed {}", path.display())); console::info(&format!("-> Sass file changed {}", path.display()));
if let Some(ref broadcaster) = broadcaster {
rebuild_done_handling( rebuild_done_handling(
&broadcaster, &broadcaster,
site.compile_sass(&site.base_path), site.compile_sass(&site.base_path),
&p.to_string_lossy(), &p.to_string_lossy(),
); );
} }
}
(ChangeKind::Config, _) => { (ChangeKind::Config, _) => {
console::info("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible."); console::info("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible.");
site = create_new_site( site = create_new_site(

View file

@ -77,11 +77,12 @@ fn main() {
::std::process::exit(1); ::std::process::exit(1);
} }
} }
let watch_only = matches.is_present("watch_only");
let output_dir = matches.value_of("output_dir").unwrap(); let output_dir = matches.value_of("output_dir").unwrap();
let base_url = matches.value_of("base_url").unwrap(); let base_url = matches.value_of("base_url").unwrap();
println!("watch_only: {}", watch_only);
console::info("Building site..."); console::info("Building site...");
match cmd::serve(interface, port, output_dir, base_url, config_file) { match cmd::serve(interface, port, output_dir, base_url, config_file, watch_only) {
Ok(()) => (), Ok(()) => (),
Err(e) => { Err(e) => {
console::unravel_errors("", &e); console::unravel_errors("", &e);