2020-09-21 08:26:41 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2020-09-11 12:19:49 -04:00
|
|
|
use crate::colors;
|
2020-10-28 07:41:18 -04:00
|
|
|
use core::task::{Context, Poll};
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-10-28 07:41:18 -04:00
|
|
|
use deno_core::futures::stream::{Stream, StreamExt};
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures::Future;
|
2020-09-11 12:19:49 -04:00
|
|
|
use notify::event::Event as NotifyEvent;
|
|
|
|
use notify::event::EventKind;
|
|
|
|
use notify::Config;
|
|
|
|
use notify::Error as NotifyError;
|
|
|
|
use notify::RecommendedWatcher;
|
|
|
|
use notify::RecursiveMode;
|
|
|
|
use notify::Watcher;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::pin::Pin;
|
2020-10-28 07:41:18 -04:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
2020-09-11 12:19:49 -04:00
|
|
|
use tokio::select;
|
2020-10-28 07:41:18 -04:00
|
|
|
use tokio::time::{interval, Interval};
|
|
|
|
|
|
|
|
const DEBOUNCE_INTERVAL_MS: Duration = Duration::from_millis(200);
|
2020-09-11 12:19:49 -04:00
|
|
|
|
|
|
|
// TODO(bartlomieju): rename
|
2020-09-14 12:48:57 -04:00
|
|
|
type WatchFuture = Pin<Box<dyn Future<Output = Result<(), AnyError>>>>;
|
2020-09-11 12:19:49 -04:00
|
|
|
|
2020-10-28 07:41:18 -04:00
|
|
|
struct Debounce {
|
|
|
|
interval: Interval,
|
|
|
|
event_detected: Arc<AtomicBool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Debounce {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
interval: interval(DEBOUNCE_INTERVAL_MS),
|
|
|
|
event_detected: Arc::new(AtomicBool::new(false)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stream for Debounce {
|
|
|
|
type Item = ();
|
|
|
|
|
|
|
|
/// Note that this never returns `Poll::Ready(None)`, which means that file watcher will be alive
|
|
|
|
/// until the Deno process is terminated.
|
|
|
|
fn poll_next(
|
|
|
|
self: Pin<&mut Self>,
|
|
|
|
cx: &mut Context,
|
|
|
|
) -> Poll<Option<Self::Item>> {
|
|
|
|
let inner = self.get_mut();
|
|
|
|
if inner.event_detected.load(Ordering::Relaxed) {
|
|
|
|
inner.event_detected.store(false, Ordering::Relaxed);
|
|
|
|
Poll::Ready(Some(()))
|
|
|
|
} else {
|
|
|
|
let _ = inner.interval.poll_tick(cx);
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 12:19:49 -04:00
|
|
|
async fn error_handler(watch_future: WatchFuture) {
|
|
|
|
let result = watch_future.await;
|
|
|
|
if let Err(err) = result {
|
|
|
|
let msg = format!("{}: {}", colors::red_bold("error"), err.to_string(),);
|
|
|
|
eprintln!("{}", msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn watch_func<F>(
|
2020-10-28 07:41:18 -04:00
|
|
|
paths: &[PathBuf],
|
2020-09-11 12:19:49 -04:00
|
|
|
closure: F,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError>
|
2020-09-11 12:19:49 -04:00
|
|
|
where
|
|
|
|
F: Fn() -> WatchFuture,
|
|
|
|
{
|
2020-10-28 07:41:18 -04:00
|
|
|
let mut debounce = Debounce::new();
|
|
|
|
// This binding is required for the watcher to work properly without being dropped.
|
|
|
|
let _watcher = new_watcher(paths, &debounce)?;
|
|
|
|
|
2020-09-11 12:19:49 -04:00
|
|
|
loop {
|
|
|
|
let func = error_handler(closure());
|
|
|
|
let mut is_file_changed = false;
|
|
|
|
select! {
|
2020-10-28 07:41:18 -04:00
|
|
|
_ = debounce.next() => {
|
|
|
|
is_file_changed = true;
|
|
|
|
info!(
|
|
|
|
"{} File change detected! Restarting!",
|
|
|
|
colors::intense_blue("Watcher"),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
_ = func => {},
|
|
|
|
}
|
2020-09-11 12:19:49 -04:00
|
|
|
if !is_file_changed {
|
|
|
|
info!(
|
|
|
|
"{} Process terminated! Restarting on file change...",
|
2020-10-28 07:41:18 -04:00
|
|
|
colors::intense_blue("Watcher"),
|
2020-09-11 12:19:49 -04:00
|
|
|
);
|
2020-10-28 07:41:18 -04:00
|
|
|
debounce.next().await;
|
2020-09-11 12:19:49 -04:00
|
|
|
info!(
|
|
|
|
"{} File change detected! Restarting!",
|
2020-10-28 07:41:18 -04:00
|
|
|
colors::intense_blue("Watcher"),
|
2020-09-11 12:19:49 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 07:41:18 -04:00
|
|
|
fn new_watcher(
|
|
|
|
paths: &[PathBuf],
|
|
|
|
debounce: &Debounce,
|
|
|
|
) -> Result<RecommendedWatcher, AnyError> {
|
|
|
|
let event_detected = Arc::clone(&debounce.event_detected);
|
2020-09-11 12:19:49 -04:00
|
|
|
|
2020-10-28 07:41:18 -04:00
|
|
|
let mut watcher: RecommendedWatcher = Watcher::new_immediate(
|
|
|
|
move |res: Result<NotifyEvent, NotifyError>| {
|
|
|
|
if let Ok(event) = res {
|
|
|
|
if matches!(event.kind, EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_))
|
|
|
|
{
|
|
|
|
event_detected.store(true, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)?;
|
2020-09-11 12:19:49 -04:00
|
|
|
|
|
|
|
watcher.configure(Config::PreciseEvents(true)).unwrap();
|
|
|
|
|
|
|
|
for path in paths {
|
|
|
|
watcher.watch(path, RecursiveMode::NonRecursive)?;
|
|
|
|
}
|
|
|
|
|
2020-10-28 07:41:18 -04:00
|
|
|
Ok(watcher)
|
2020-09-11 12:19:49 -04:00
|
|
|
}
|