2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-09-21 08:26:41 -04:00
|
|
|
|
2023-06-14 18:29:19 -04:00
|
|
|
use crate::args::Flags;
|
2020-09-11 12:19:49 -04:00
|
|
|
use crate::colors;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::fs::canonicalize_path;
|
2021-11-01 16:22:08 -04:00
|
|
|
|
2024-03-27 18:47:46 -04:00
|
|
|
use deno_config::glob::PathOrPatternSet;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-07-13 16:01:09 -04:00
|
|
|
use deno_core::error::JsError;
|
2021-01-13 00:48:55 -05:00
|
|
|
use deno_core::futures::Future;
|
2023-10-19 01:05:00 -04:00
|
|
|
use deno_core::futures::FutureExt;
|
2023-10-30 20:25:58 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2022-09-02 16:53:23 -04:00
|
|
|
use deno_runtime::fmt_errors::format_js_error;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::info;
|
2020-09-11 12:19:49 -04:00
|
|
|
use notify::event::Event as NotifyEvent;
|
|
|
|
use notify::event::EventKind;
|
|
|
|
use notify::Error as NotifyError;
|
|
|
|
use notify::RecommendedWatcher;
|
|
|
|
use notify::RecursiveMode;
|
|
|
|
use notify::Watcher;
|
2023-10-30 20:25:58 -04:00
|
|
|
use std::cell::RefCell;
|
2021-05-10 02:06:13 -04:00
|
|
|
use std::collections::HashSet;
|
2023-08-25 09:43:07 -04:00
|
|
|
use std::io::IsTerminal;
|
2020-09-11 12:19:49 -04:00
|
|
|
use std::path::PathBuf;
|
2023-10-30 20:25:58 -04:00
|
|
|
use std::rc::Rc;
|
2020-10-28 07:41:18 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
2020-09-11 12:19:49 -04:00
|
|
|
use tokio::select;
|
2021-12-09 20:24:37 -05:00
|
|
|
use tokio::sync::mpsc;
|
2022-06-08 06:07:25 -04:00
|
|
|
use tokio::sync::mpsc::UnboundedReceiver;
|
2021-01-12 02:50:02 -05:00
|
|
|
use tokio::time::sleep;
|
2020-10-28 07:41:18 -04:00
|
|
|
|
2021-12-10 19:12:25 -05:00
|
|
|
const CLEAR_SCREEN: &str = "\x1B[2J\x1B[1;1H";
|
2021-01-13 00:48:55 -05:00
|
|
|
const DEBOUNCE_INTERVAL: Duration = Duration::from_millis(200);
|
2020-09-11 12:19:49 -04:00
|
|
|
|
2021-12-09 20:24:37 -05:00
|
|
|
struct DebouncedReceiver {
|
2022-04-19 10:12:30 -04:00
|
|
|
// The `recv()` call could be used in a tokio `select!` macro,
|
|
|
|
// and so we store this state on the struct to ensure we don't
|
|
|
|
// lose items if a `recv()` never completes
|
|
|
|
received_items: HashSet<PathBuf>,
|
2022-07-14 17:52:44 -04:00
|
|
|
receiver: UnboundedReceiver<Vec<PathBuf>>,
|
2020-10-28 07:41:18 -04:00
|
|
|
}
|
|
|
|
|
2021-12-09 20:24:37 -05:00
|
|
|
impl DebouncedReceiver {
|
|
|
|
fn new_with_sender() -> (Arc<mpsc::UnboundedSender<Vec<PathBuf>>>, Self) {
|
|
|
|
let (sender, receiver) = mpsc::unbounded_channel();
|
2022-04-19 10:12:30 -04:00
|
|
|
(
|
|
|
|
Arc::new(sender),
|
|
|
|
Self {
|
|
|
|
receiver,
|
|
|
|
received_items: HashSet::new(),
|
|
|
|
},
|
|
|
|
)
|
2020-10-28 07:41:18 -04:00
|
|
|
}
|
|
|
|
|
2021-12-09 20:24:37 -05:00
|
|
|
async fn recv(&mut self) -> Option<Vec<PathBuf>> {
|
2022-04-19 10:12:30 -04:00
|
|
|
if self.received_items.is_empty() {
|
|
|
|
self
|
|
|
|
.received_items
|
|
|
|
.extend(self.receiver.recv().await?.into_iter());
|
|
|
|
}
|
|
|
|
|
2021-12-09 20:24:37 -05:00
|
|
|
loop {
|
2022-07-14 17:52:44 -04:00
|
|
|
select! {
|
2021-12-09 20:24:37 -05:00
|
|
|
items = self.receiver.recv() => {
|
2022-04-19 10:12:30 -04:00
|
|
|
self.received_items.extend(items?);
|
2021-12-09 20:24:37 -05:00
|
|
|
}
|
|
|
|
_ = sleep(DEBOUNCE_INTERVAL) => {
|
2022-04-19 10:12:30 -04:00
|
|
|
return Some(self.received_items.drain().collect());
|
2021-12-09 20:24:37 -05:00
|
|
|
}
|
2021-05-10 02:06:13 -04:00
|
|
|
}
|
2020-10-28 07:41:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-08 22:45:06 -04:00
|
|
|
#[allow(clippy::print_stderr)]
|
2023-06-14 18:29:19 -04:00
|
|
|
async fn error_handler<F>(watch_future: F) -> bool
|
2021-05-10 02:06:13 -04:00
|
|
|
where
|
|
|
|
F: Future<Output = Result<(), AnyError>>,
|
|
|
|
{
|
2020-09-11 12:19:49 -04:00
|
|
|
let result = watch_future.await;
|
|
|
|
if let Err(err) = result {
|
2022-07-13 16:01:09 -04:00
|
|
|
let error_string = match err.downcast_ref::<JsError>() {
|
|
|
|
Some(e) => format_js_error(e),
|
2023-01-27 10:43:16 -05:00
|
|
|
None => format!("{err:?}"),
|
2022-07-13 16:01:09 -04:00
|
|
|
};
|
|
|
|
eprintln!(
|
|
|
|
"{}: {}",
|
|
|
|
colors::red_bold("error"),
|
|
|
|
error_string.trim_start_matches("error: ")
|
|
|
|
);
|
2023-06-14 18:29:19 -04:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
2020-11-22 15:45:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 11:39:39 -05:00
|
|
|
pub struct PrintConfig {
|
2023-10-30 20:25:58 -04:00
|
|
|
banner: &'static str,
|
|
|
|
/// Printing watcher status to terminal.
|
|
|
|
job_name: &'static str,
|
|
|
|
/// Determine whether to clear the terminal screen; applicable to TTY environments only.
|
|
|
|
clear_screen: bool,
|
2022-01-31 11:39:39 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 20:25:58 -04:00
|
|
|
impl PrintConfig {
|
|
|
|
/// By default `PrintConfig` uses "Watcher" as a banner name that will
|
|
|
|
/// be printed in color. If you need to customize it, use
|
|
|
|
/// `PrintConfig::new_with_banner` instead.
|
|
|
|
pub fn new(job_name: &'static str, clear_screen: bool) -> Self {
|
|
|
|
Self {
|
|
|
|
banner: "Watcher",
|
|
|
|
job_name,
|
|
|
|
clear_screen,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_banner(
|
|
|
|
banner: &'static str,
|
|
|
|
job_name: &'static str,
|
|
|
|
clear_screen: bool,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
banner,
|
|
|
|
job_name,
|
|
|
|
clear_screen,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_print_after_restart_fn(
|
|
|
|
banner: &'static str,
|
|
|
|
clear_screen: bool,
|
|
|
|
) -> impl Fn() {
|
2022-06-08 06:07:25 -04:00
|
|
|
move || {
|
2024-05-08 22:45:06 -04:00
|
|
|
#[allow(clippy::print_stderr)]
|
2023-08-25 09:43:07 -04:00
|
|
|
if clear_screen && std::io::stderr().is_terminal() {
|
2024-05-08 22:45:06 -04:00
|
|
|
eprint!("{}", CLEAR_SCREEN);
|
2022-06-08 06:07:25 -04:00
|
|
|
}
|
|
|
|
info!(
|
|
|
|
"{} File change detected! Restarting!",
|
2023-10-30 20:25:58 -04:00
|
|
|
colors::intense_blue(banner),
|
2022-06-08 06:07:25 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 01:05:00 -04:00
|
|
|
/// An interface to interact with Deno's CLI file watcher.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct WatcherCommunicator {
|
|
|
|
/// Send a list of paths that should be watched for changes.
|
|
|
|
paths_to_watch_tx: tokio::sync::mpsc::UnboundedSender<Vec<PathBuf>>,
|
|
|
|
|
|
|
|
/// Listen for a list of paths that were changed.
|
|
|
|
changed_paths_rx: tokio::sync::broadcast::Receiver<Option<Vec<PathBuf>>>,
|
|
|
|
|
|
|
|
/// Send a message to force a restart.
|
|
|
|
restart_tx: tokio::sync::mpsc::UnboundedSender<()>,
|
|
|
|
|
2023-10-30 20:25:58 -04:00
|
|
|
restart_mode: Mutex<WatcherRestartMode>,
|
|
|
|
|
|
|
|
banner: String,
|
2023-10-19 01:05:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WatcherCommunicator {
|
|
|
|
pub fn watch_paths(&self, paths: Vec<PathBuf>) -> Result<(), AnyError> {
|
|
|
|
self.paths_to_watch_tx.send(paths).map_err(AnyError::from)
|
|
|
|
}
|
2023-10-30 20:25:58 -04:00
|
|
|
|
|
|
|
pub fn force_restart(&self) -> Result<(), AnyError> {
|
|
|
|
// Change back to automatic mode, so that HMR can set up watching
|
|
|
|
// from scratch.
|
|
|
|
*self.restart_mode.lock() = WatcherRestartMode::Automatic;
|
|
|
|
self.restart_tx.send(()).map_err(AnyError::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn watch_for_changed_paths(
|
|
|
|
&self,
|
|
|
|
) -> Result<Option<Vec<PathBuf>>, AnyError> {
|
|
|
|
let mut rx = self.changed_paths_rx.resubscribe();
|
|
|
|
rx.recv().await.map_err(AnyError::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn change_restart_mode(&self, restart_mode: WatcherRestartMode) {
|
|
|
|
*self.restart_mode.lock() = restart_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print(&self, msg: String) {
|
|
|
|
log::info!("{} {}", self.banner, msg);
|
|
|
|
}
|
2023-10-19 01:05:00 -04:00
|
|
|
}
|
|
|
|
|
2022-06-08 06:07:25 -04:00
|
|
|
/// Creates a file watcher.
|
|
|
|
///
|
|
|
|
/// - `operation` is the actual operation we want to run every time the watcher detects file
|
|
|
|
/// changes. For example, in the case where we would like to bundle, then `operation` would
|
|
|
|
/// have the logic for it like bundling the code.
|
2023-06-14 18:29:19 -04:00
|
|
|
pub async fn watch_func<O, F>(
|
2023-10-19 01:05:00 -04:00
|
|
|
flags: Flags,
|
|
|
|
print_config: PrintConfig,
|
|
|
|
operation: O,
|
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
O: FnMut(
|
|
|
|
Flags,
|
2023-10-30 20:25:58 -04:00
|
|
|
Arc<WatcherCommunicator>,
|
2023-10-19 01:05:00 -04:00
|
|
|
Option<Vec<PathBuf>>,
|
|
|
|
) -> Result<F, AnyError>,
|
|
|
|
F: Future<Output = Result<(), AnyError>>,
|
|
|
|
{
|
|
|
|
let fut = watch_recv(
|
|
|
|
flags,
|
|
|
|
print_config,
|
|
|
|
WatcherRestartMode::Automatic,
|
|
|
|
operation,
|
|
|
|
)
|
|
|
|
.boxed_local();
|
|
|
|
|
|
|
|
fut.await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub enum WatcherRestartMode {
|
|
|
|
/// When a file path changes the process is restarted.
|
|
|
|
Automatic,
|
|
|
|
|
|
|
|
/// When a file path changes the caller will trigger a restart, using
|
2023-10-30 20:25:58 -04:00
|
|
|
/// `WatcherInterface.restart_tx`.
|
2023-10-19 01:05:00 -04:00
|
|
|
Manual,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a file watcher.
|
|
|
|
///
|
|
|
|
/// - `operation` is the actual operation we want to run every time the watcher detects file
|
|
|
|
/// changes. For example, in the case where we would like to bundle, then `operation` would
|
|
|
|
/// have the logic for it like bundling the code.
|
|
|
|
pub async fn watch_recv<O, F>(
|
2023-06-14 18:29:19 -04:00
|
|
|
mut flags: Flags,
|
2022-06-08 06:07:25 -04:00
|
|
|
print_config: PrintConfig,
|
2023-10-19 01:05:00 -04:00
|
|
|
restart_mode: WatcherRestartMode,
|
2023-06-14 18:29:19 -04:00
|
|
|
mut operation: O,
|
2022-06-08 06:07:25 -04:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
2023-06-14 18:29:19 -04:00
|
|
|
O: FnMut(
|
|
|
|
Flags,
|
2023-10-30 20:25:58 -04:00
|
|
|
Arc<WatcherCommunicator>,
|
2023-06-14 18:29:19 -04:00
|
|
|
Option<Vec<PathBuf>>,
|
|
|
|
) -> Result<F, AnyError>,
|
2022-06-08 06:07:25 -04:00
|
|
|
F: Future<Output = Result<(), AnyError>>,
|
|
|
|
{
|
2024-03-27 18:47:46 -04:00
|
|
|
let exclude_set = flags.resolve_watch_exclude_set()?;
|
2023-10-19 01:05:00 -04:00
|
|
|
let (paths_to_watch_tx, mut paths_to_watch_rx) =
|
2023-06-14 18:29:19 -04:00
|
|
|
tokio::sync::mpsc::unbounded_channel();
|
2023-10-19 01:05:00 -04:00
|
|
|
let (restart_tx, mut restart_rx) = tokio::sync::mpsc::unbounded_channel();
|
|
|
|
let (changed_paths_tx, changed_paths_rx) = tokio::sync::broadcast::channel(4);
|
2022-06-08 06:07:25 -04:00
|
|
|
let (watcher_sender, mut watcher_receiver) =
|
|
|
|
DebouncedReceiver::new_with_sender();
|
|
|
|
|
|
|
|
let PrintConfig {
|
2023-10-30 20:25:58 -04:00
|
|
|
banner,
|
2022-06-08 06:07:25 -04:00
|
|
|
job_name,
|
|
|
|
clear_screen,
|
|
|
|
} = print_config;
|
|
|
|
|
2023-10-30 20:25:58 -04:00
|
|
|
let print_after_restart = create_print_after_restart_fn(banner, clear_screen);
|
|
|
|
let watcher_communicator = Arc::new(WatcherCommunicator {
|
2023-10-19 01:05:00 -04:00
|
|
|
paths_to_watch_tx: paths_to_watch_tx.clone(),
|
|
|
|
changed_paths_rx: changed_paths_rx.resubscribe(),
|
|
|
|
restart_tx: restart_tx.clone(),
|
2023-10-30 20:25:58 -04:00
|
|
|
restart_mode: Mutex::new(restart_mode),
|
|
|
|
banner: colors::intense_blue(banner).to_string(),
|
|
|
|
});
|
|
|
|
info!("{} {} started.", colors::intense_blue(banner), job_name);
|
|
|
|
|
|
|
|
let changed_paths = Rc::new(RefCell::new(None));
|
|
|
|
let changed_paths_ = changed_paths.clone();
|
|
|
|
let watcher_ = watcher_communicator.clone();
|
|
|
|
|
|
|
|
deno_core::unsync::spawn(async move {
|
|
|
|
loop {
|
|
|
|
let received_changed_paths = watcher_receiver.recv().await;
|
|
|
|
*changed_paths_.borrow_mut() = received_changed_paths.clone();
|
|
|
|
|
|
|
|
match *watcher_.restart_mode.lock() {
|
|
|
|
WatcherRestartMode::Automatic => {
|
|
|
|
let _ = restart_tx.send(());
|
|
|
|
}
|
|
|
|
WatcherRestartMode::Manual => {
|
|
|
|
// TODO(bartlomieju): should we fail on sending changed paths?
|
|
|
|
let _ = changed_paths_tx.send(received_changed_paths);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-06-08 06:07:25 -04:00
|
|
|
|
|
|
|
loop {
|
2023-05-08 03:52:56 -04:00
|
|
|
// We may need to give the runtime a tick to settle, as cancellations may need to propagate
|
|
|
|
// to tasks. We choose yielding 10 times to the runtime as a decent heuristic. If watch tests
|
|
|
|
// start to fail, this may need to be increased.
|
|
|
|
for _ in 0..10 {
|
|
|
|
tokio::task::yield_now().await;
|
|
|
|
}
|
|
|
|
|
2022-06-08 06:07:25 -04:00
|
|
|
let mut watcher = new_watcher(watcher_sender.clone())?;
|
2024-03-27 18:47:46 -04:00
|
|
|
consume_paths_to_watch(&mut watcher, &mut paths_to_watch_rx, &exclude_set);
|
2022-06-08 06:07:25 -04:00
|
|
|
|
|
|
|
let receiver_future = async {
|
|
|
|
loop {
|
2023-10-19 01:05:00 -04:00
|
|
|
let maybe_paths = paths_to_watch_rx.recv().await;
|
2024-03-27 18:47:46 -04:00
|
|
|
add_paths_to_watcher(&mut watcher, &maybe_paths.unwrap(), &exclude_set);
|
2022-06-08 06:07:25 -04:00
|
|
|
}
|
|
|
|
};
|
2023-06-14 18:29:19 -04:00
|
|
|
let operation_future = error_handler(operation(
|
|
|
|
flags.clone(),
|
2023-10-19 01:05:00 -04:00
|
|
|
watcher_communicator.clone(),
|
2023-10-30 20:25:58 -04:00
|
|
|
changed_paths.borrow_mut().take(),
|
2023-06-14 18:29:19 -04:00
|
|
|
)?);
|
|
|
|
|
|
|
|
// don't reload dependencies after the first run
|
|
|
|
flags.reload = false;
|
2022-06-08 06:07:25 -04:00
|
|
|
|
|
|
|
select! {
|
|
|
|
_ = receiver_future => {},
|
2023-10-19 01:05:00 -04:00
|
|
|
_ = restart_rx.recv() => {
|
2022-06-08 06:07:25 -04:00
|
|
|
print_after_restart();
|
|
|
|
continue;
|
|
|
|
},
|
2023-06-14 18:29:19 -04:00
|
|
|
success = operation_future => {
|
2024-03-27 18:47:46 -04:00
|
|
|
consume_paths_to_watch(&mut watcher, &mut paths_to_watch_rx, &exclude_set);
|
2022-06-08 06:07:25 -04:00
|
|
|
// TODO(bartlomieju): print exit code here?
|
|
|
|
info!(
|
2023-06-14 18:29:19 -04:00
|
|
|
"{} {} {}. Restarting on file change...",
|
2023-10-30 20:25:58 -04:00
|
|
|
colors::intense_blue(banner),
|
2022-06-08 06:07:25 -04:00
|
|
|
job_name,
|
2023-06-14 18:29:19 -04:00
|
|
|
if success {
|
|
|
|
"finished"
|
|
|
|
} else {
|
|
|
|
"failed"
|
|
|
|
}
|
2022-06-08 06:07:25 -04:00
|
|
|
);
|
|
|
|
},
|
2024-03-27 18:47:46 -04:00
|
|
|
}
|
2022-06-08 06:07:25 -04:00
|
|
|
let receiver_future = async {
|
|
|
|
loop {
|
2023-10-19 01:05:00 -04:00
|
|
|
let maybe_paths = paths_to_watch_rx.recv().await;
|
2024-03-27 18:47:46 -04:00
|
|
|
add_paths_to_watcher(&mut watcher, &maybe_paths.unwrap(), &exclude_set);
|
2022-06-08 06:07:25 -04:00
|
|
|
}
|
|
|
|
};
|
2023-10-19 01:05:00 -04:00
|
|
|
|
|
|
|
// If we got this far, it means that the `operation` has finished; let's wait
|
|
|
|
// and see if there are any new paths to watch received or any of the already
|
|
|
|
// watched paths has changed.
|
2022-06-08 06:07:25 -04:00
|
|
|
select! {
|
|
|
|
_ = receiver_future => {},
|
2023-10-30 20:25:58 -04:00
|
|
|
_ = restart_rx.recv() => {
|
2022-06-08 06:07:25 -04:00
|
|
|
print_after_restart();
|
|
|
|
continue;
|
|
|
|
},
|
2024-03-27 18:47:46 -04:00
|
|
|
}
|
2022-06-08 06:07:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 07:41:18 -04:00
|
|
|
fn new_watcher(
|
2021-12-09 20:24:37 -05:00
|
|
|
sender: Arc<mpsc::UnboundedSender<Vec<PathBuf>>>,
|
2020-10-28 07:41:18 -04:00
|
|
|
) -> Result<RecommendedWatcher, AnyError> {
|
2023-10-19 01:05:00 -04:00
|
|
|
Ok(Watcher::new(
|
2022-09-16 19:11:30 -04:00
|
|
|
move |res: Result<NotifyEvent, NotifyError>| {
|
2023-10-19 01:05:00 -04:00
|
|
|
let Ok(event) = res else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
if !matches!(
|
|
|
|
event.kind,
|
|
|
|
EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_)
|
|
|
|
) {
|
|
|
|
return;
|
2020-10-28 07:41:18 -04:00
|
|
|
}
|
2023-10-19 01:05:00 -04:00
|
|
|
|
|
|
|
let paths = event
|
|
|
|
.paths
|
|
|
|
.iter()
|
|
|
|
.filter_map(|path| canonicalize_path(path).ok())
|
|
|
|
.collect();
|
2024-03-27 18:47:46 -04:00
|
|
|
|
2023-10-19 01:05:00 -04:00
|
|
|
sender.send(paths).unwrap();
|
2022-09-16 19:11:30 -04:00
|
|
|
},
|
|
|
|
Default::default(),
|
2023-10-19 01:05:00 -04:00
|
|
|
)?)
|
2022-06-08 06:07:25 -04:00
|
|
|
}
|
|
|
|
|
2024-03-27 18:47:46 -04:00
|
|
|
fn add_paths_to_watcher(
|
|
|
|
watcher: &mut RecommendedWatcher,
|
|
|
|
paths: &[PathBuf],
|
|
|
|
paths_to_exclude: &PathOrPatternSet,
|
|
|
|
) {
|
2022-06-08 06:07:25 -04:00
|
|
|
// Ignore any error e.g. `PathNotFound`
|
2024-03-27 18:47:46 -04:00
|
|
|
let mut watched_paths = Vec::new();
|
|
|
|
|
2020-09-11 12:19:49 -04:00
|
|
|
for path in paths {
|
2024-03-27 18:47:46 -04:00
|
|
|
if paths_to_exclude.matches_path(path) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
watched_paths.push(path.clone());
|
2021-05-10 02:06:13 -04:00
|
|
|
let _ = watcher.watch(path, RecursiveMode::Recursive);
|
2020-09-11 12:19:49 -04:00
|
|
|
}
|
2024-03-27 18:47:46 -04:00
|
|
|
log::debug!("Watching paths: {:?}", watched_paths);
|
2020-09-11 12:19:49 -04:00
|
|
|
}
|
2023-10-19 01:05:00 -04:00
|
|
|
|
|
|
|
fn consume_paths_to_watch(
|
|
|
|
watcher: &mut RecommendedWatcher,
|
|
|
|
receiver: &mut UnboundedReceiver<Vec<PathBuf>>,
|
2024-03-27 18:47:46 -04:00
|
|
|
exclude_set: &PathOrPatternSet,
|
2023-10-19 01:05:00 -04:00
|
|
|
) {
|
|
|
|
loop {
|
|
|
|
match receiver.try_recv() {
|
|
|
|
Ok(paths) => {
|
2024-03-27 18:47:46 -04:00
|
|
|
add_paths_to_watcher(watcher, &paths, exclude_set);
|
2023-10-19 01:05:00 -04:00
|
|
|
}
|
|
|
|
Err(e) => match e {
|
|
|
|
mpsc::error::TryRecvError::Empty => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// there must be at least one receiver alive
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|