Library/Fn/SWC/Watch/
Compile.rs1use std::io::Write;
6#[tracing::instrument(skip(options))]
7pub async fn Fn(options:crate::Struct::SWC::Option) -> anyhow::Result<()> {
8 let compiler = std::sync::Arc::new(crate::Fn::OXC::Compiler::Compiler::new(options.config.clone()));
9
10 let input_base = options.entry[0][0].clone();
12 let output_base = options.output.clone();
13 let pattern = options.pattern.clone();
14
15 println!("Starting watch compilation from {} to {}", input_base, output_base);
16
17 let ts_files:Vec<String> = walkdir::WalkDir::new(&input_base)
19 .follow_links(true)
20 .into_iter()
21 .filter_map(|e| {
22 let entry = e.ok()?;
23 let path = entry.path();
24 if path.is_file() && path.to_string_lossy().ends_with(&pattern) {
25 Some(path.to_string_lossy().to_string())
26 } else {
27 None
28 }
29 })
30 .collect();
31
32 println!("Found {} TypeScript files in {}", ts_files.len(), input_base);
33
34 let mut count = 0;
36 let mut error = 0;
37
38 for file_path in ts_files {
39 print!(".");
40 std::io::stdout().flush().unwrap();
41
42 match tokio::fs::read_to_string(&file_path).await {
43 Ok(input) => {
44 let input_path = std::path::Path::new(&file_path);
46 let base_path = std::path::Path::new(&input_base);
47 let relative_path = input_path.strip_prefix(base_path).unwrap_or(input_path);
48
49 let output_path = std::path::Path::new(&output_base).join(relative_path).with_extension("js");
51
52 match compiler.compile_file_to(&file_path, input, &output_path, options.use_define_for_class_fields) {
53 Ok(output) => {
54 debug!("Compiled: {} -> {}", file_path, output);
55 count += 1;
56 },
57 Err(e) => {
58 error!("Compilation error for {}: {}", file_path, e);
59 error += 1;
60 },
61 }
62 },
63 Err(e) => {
64 error!("Failed to read file {}: {}", file_path, e);
65 error += 1;
66 },
67 }
68 }
69
70 println!();
71
72 let outlook = compiler.outlook.lock().unwrap();
73
74 info!(
75 "Watch compilation complete. Processed {} files in {:?}. {} successful, {} failed.",
76 outlook.count, outlook.elapsed, count, error
77 );
78
79 Ok(())
80}
81
82use tracing::{debug, error, info};