|
| 1 | +use std::{env, fs, path::Path, process}; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + config::get_compiler_options, |
| 7 | + parsers::{extract_default_css_imports, extract_used_classes}, |
| 8 | + utils::{list_files_in_directory, process_relative_import, replace_aliases}, |
| 9 | +}; |
| 10 | + |
| 11 | +pub fn get_class_usages() -> Result<()> { |
| 12 | + const COLOR_RED: &str = "\x1b[31m"; |
| 13 | + const COLOR_RESET: &str = "\u{001B}[0m"; |
| 14 | + |
| 15 | + let args: Vec<String> = env::args().collect(); |
| 16 | + |
| 17 | + let file = args.get(2).unwrap_or_else(|| { |
| 18 | + eprintln!("Path to the file must be provided"); |
| 19 | + process::exit(1); |
| 20 | + }); |
| 21 | + |
| 22 | + let workplace_path = env::current_dir()?; |
| 23 | + let target_file = match Path::new(file).strip_prefix(workplace_path) { |
| 24 | + Ok(f) => f, |
| 25 | + Err(_) => { |
| 26 | + eprintln!("Path to the file must be relative to the current directory"); |
| 27 | + process::exit(1); |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + let class_name = args.get(3).unwrap_or_else(|| { |
| 32 | + eprintln!("Class name must be provided"); |
| 33 | + process::exit(1); |
| 34 | + }); |
| 35 | + |
| 36 | + let tsconfig = get_compiler_options().unwrap_or_else(|e| { |
| 37 | + eprintln!( |
| 38 | + "\n{}Error{}: Could not load tsconfig.json. Is the provided directory a typescript project? ({})", |
| 39 | + COLOR_RED, COLOR_RESET, e |
| 40 | + ); |
| 41 | + process::exit(1); |
| 42 | + }); |
| 43 | + |
| 44 | + let dir = list_files_in_directory(Path::new(".").to_path_buf(), tsconfig.exclude); |
| 45 | + for entry in &dir { |
| 46 | + let path = entry.replace("\\", "/"); |
| 47 | + |
| 48 | + if path.ends_with(".tsx") || path.ends_with(".jsx") { |
| 49 | + let code = fs::read_to_string(entry)?; |
| 50 | + let imported_css = extract_default_css_imports(&code).unwrap_or_else(|e| { |
| 51 | + eprintln!("Could not parse file: {}\n{}", entry, e); |
| 52 | + process::exit(1); |
| 53 | + }); |
| 54 | + |
| 55 | + for (mut style_path, class_names) in imported_css { |
| 56 | + process_relative_import(Path::new(entry), &mut style_path)?; |
| 57 | + replace_aliases(&mut style_path, tsconfig.compiler_options.paths.clone()); |
| 58 | + |
| 59 | + if target_file.to_string_lossy().replace("\\", "/") != style_path.replace("./", "") |
| 60 | + { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + let used_fields = extract_used_classes(&code, &class_names, path.clone()) |
| 65 | + .unwrap_or_else(|e| { |
| 66 | + eprintln!("Could not parse file: {}\n{}", entry, e); |
| 67 | + process::exit(1); |
| 68 | + }); |
| 69 | + |
| 70 | + for field in used_fields { |
| 71 | + if field.class_name == *class_name { |
| 72 | + println!( |
| 73 | + "{}:{}:{}:{}", |
| 74 | + path, |
| 75 | + field.line, |
| 76 | + field.column, |
| 77 | + field.class_name.len() |
| 78 | + ); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + Ok(()) |
| 85 | +} |
0 commit comments