Skip to main content

morph_cli/utils/
path.rs

1use std::path::{Path, PathBuf};
2
3pub fn resolve_relative_import(current_file: &Path, import_src: &str) -> Option<PathBuf> {
4    let parent = current_file.parent().unwrap_or(Path::new("."));
5    let base_path = parent.join(import_src);
6    
7    // Check direct file
8    if base_path.exists() && base_path.is_file() { return Some(base_path); }
9    
10    // Check with extensions
11    for ext in &["js", "ts", "jsx", "tsx", "mjs", "cjs"] {
12        let p = base_path.with_extension(ext);
13        if p.exists() { return Some(p); }
14    }
15    
16    // Check index files
17    let index_path = base_path.join("index");
18    for ext in &["js", "ts", "jsx", "tsx", "mjs", "cjs"] {
19        let p = index_path.with_extension(ext);
20        if p.exists() { return Some(p); }
21    }
22
23    None
24}