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
22	watcher.watch(path.as_ref(), notify::RecursiveMode::Recursive)?;
23
24	while let Some(result) = rx.recv().await {
25		match result {
26			Ok(event) => {
27				if let notify::Event {
28					kind: notify::EventKind::Modify(notify::event::ModifyKind::Data(_)),
29
30					paths,
31					..
32				} = event
33				{
34					for path in paths {
35						if path.extension().map_or(false, |ext| ext == "ts") {
36							let options = options.clone();
37
38							tokio::task::spawn_blocking(move || {
39								let rt = tokio::runtime::Handle::current();
40
41								rt.block_on(async {
42									if let Err(e) = Compile::Fn(
43										crate::Struct::SWC::Option {
44											entry:vec![vec![path.to_string_lossy().to_string()]],
45											..options
46										},
47										false, // parallel = false for sequential processing
48									)
49									.await
50									{
51										error!("Compilation error: {}", e);
52									}
53								})
54							});
55						}
56					}
57				}
58			},
59
60			Err(e) => error!("Watch error: {:?}", e),
61		}
62	}
63
64	Ok(())
65}
66
67use tracing::error;