1use std::{
11 io::Write,
12 sync::atomic::{AtomicUsize, Ordering},
13};
14
15use tracing::{debug, error, info, trace};
16
17static FILE_PROCESS_COUNT:AtomicUsize = AtomicUsize::new(0);
18
19fn get_nesting_depth(path:&str) -> usize {
21 path.chars().filter(|&c| c == std::path::MAIN_SEPARATOR || c == '/').count()
22}
23
24#[tracing::instrument(skip(options))]
25pub async fn Fn(options:crate::Struct::SWC::Option, _parallel:bool) -> anyhow::Result<()> {
34 info!("=== OXC Compilation START ===");
35 tracing_subscriber::fmt::init();
36
37 let compiler = std::sync::Arc::new(crate::Fn::OXC::Compiler::Compiler::new(options.config.clone()));
38
39 let input_base = options.entry[0][0].clone();
41 let output_base = options.output.clone();
42 let pattern = options.pattern.clone();
43
44 info!("Compilation from {} to {}", input_base, output_base);
45 debug!("Pattern: {}, Parallel: {}", pattern, _parallel);
46
47 let walk_start = std::time::Instant::now();
49 let ts_files:Vec<String> = walkdir::WalkDir::new(&input_base)
50 .follow_links(true)
51 .into_iter()
52 .filter_map(|e| {
53 let entry = e.ok()?;
54 let path = entry.path();
55 if path.is_file() && path.to_string_lossy().ends_with(&pattern) {
56 Some(path.to_string_lossy().to_string())
57 } else {
58 None
59 }
60 })
61 .collect();
62 info!(
63 "File discovery completed in {:?}, found {} TypeScript files",
64 walk_start.elapsed(),
65 ts_files.len()
66 );
67
68 let mut depth_dist = std::collections::HashMap::new();
70 for f in &ts_files {
71 let depth = get_nesting_depth(f);
72 *depth_dist.entry(depth).or_insert(0) += 1;
73 }
74 debug!("File distribution by depth: {:?}", depth_dist);
75
76 let mut sorted_files = ts_files.clone();
78 sorted_files.sort_by_key(|f| get_nesting_depth(f));
79
80 trace!("File list (sorted by depth):");
81 for (i, f) in sorted_files.iter().enumerate() {
82 trace!(" [{}] {} (depth={})", i, f, get_nesting_depth(f));
83 }
84
85 let mut count = 0;
87 let mut error = 0;
88 let mut current_file = 0;
89
90 let total_files = sorted_files.len();
91 info!("Starting sequential file processing ({} files)...", total_files);
92 for file_path in sorted_files {
93 current_file += 1;
94 let file_id = FILE_PROCESS_COUNT.fetch_add(1, Ordering::SeqCst);
95 let depth = get_nesting_depth(&file_path);
96
97 print!(".");
98 std::io::stdout().flush().unwrap();
99
100 info!(
101 "[File #{file_id}] Processing [{}/{}]: {} (depth={})",
102 current_file, total_files, file_path, depth
103 );
104
105 match tokio::fs::read_to_string(&file_path).await {
106 Ok(input) => {
107 trace!("[File #{file_id}] Read {} bytes", input.len());
108
109 let input_path = std::path::Path::new(&file_path);
111 let base_path = std::path::Path::new(&input_base);
112 let relative_path = input_path.strip_prefix(base_path).unwrap_or(input_path);
113
114 let output_path = std::path::Path::new(&output_base).join(relative_path).with_extension("js");
116
117 debug!("[File #{file_id}] Output path: {}", output_path.display());
118
119 let compile_start = std::time::Instant::now();
120 match compiler.compile_file_to(&file_path, input, &output_path, options.use_define_for_class_fields) {
121 Ok(output) => {
122 info!(
123 "[File #{file_id}] SUCCESS in {:?}: {} -> {}",
124 compile_start.elapsed(),
125 file_path,
126 output
127 );
128 count += 1;
129 },
130 Err(e) => {
131 error!(
132 "[File #{file_id}] FAILED in {:?}: {} - Error: {}",
133 compile_start.elapsed(),
134 file_path,
135 e
136 );
137 error += 1;
138 },
139 }
140 },
141 Err(e) => {
142 error!("[File #{file_id}] READ FAILED: {} - Error: {}", file_path, e);
143 error += 1;
144 },
145 }
146 }
147
148 println!();
149
150 let outlook = compiler.outlook.lock().unwrap();
151
152 info!("=== OXC Compilation COMPLETE ===");
153 info!(
154 "Total: {} files, Successful: {}, Failed: {}, Time: {:?}",
155 outlook.count, count, error, outlook.elapsed
156 );
157
158 println!("\n=== Compilation Summary ===");
160 println!("Total files processed: {}", outlook.count);
161 println!("Successful: {}", count);
162 println!("Failed: {}", error);
163 println!("Time elapsed: {:?}\n", outlook.elapsed);
164
165 Ok(())
166}