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