Skip to main content

Library/Fn/OXC/
Watch.rs

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