Rest/Fn/SWC/
Watch.rs

1pub mod Compile;
2
3#[tracing::instrument]
4pub async fn Fn(Path: PathBuf, Option: Option) -> anyhow::Result<()> {
5	let (tx, mut rx) = mpsc::unbounded_channel();
6
7	let mut watcher = notify::RecommendedWatcher::new(
8		move |res| {
9			let _ = futures::executor::block_on(async {
10				tx.send(res).unwrap();
11			});
12		},
13		notify::Config::default(),
14	)?;
15	
16	use notify::Watcher; // trait import
17	watcher.watch(Path.as_ref(), notify::RecursiveMode::Recursive)?;
18
19	while let Some(Result) = rx.recv().await {
20		match Result {
21			Ok(event) => {
22				if let notify::Event {
23					kind: notify::EventKind::Modify(notify::event::ModifyKind::Data(_)),
24					paths,
25					..
26				} = event
27				{
28					for path in paths {
29						if path.extension().map_or(false, |ext| ext == "ts") {
30							let Option = Option.clone();
31							tokio::task::spawn_blocking(move || {
32								let rt = tokio::runtime::Handle::current();
33								rt.block_on(async {
34									if let Err(e) = Compile::Fn(crate::Struct::SWC::Option {
35										entry: vec![vec![path.to_string_lossy().to_string()]],
36										..Option
37									})
38									.await
39									{
40										error!("Compilation error: {}", e);
41									}
42								})
43							});
44						}
45					}
46				}
47			},
48			Err(e) => error!("Watch error: {:?}", e),
49		}
50	}
51
52	Ok(())
53}
54
55use std::path::PathBuf;
56use tokio::sync::mpsc;
57use tracing::error;
58use crate::Struct::SWC::Option;