23 lines
712 B
Rust
23 lines
712 B
Rust
use anyhow::{anyhow, Result};
|
|
use std::path::{Path, PathBuf};
|
|
|
|
/// Helper to resolve import paths (e.g., converting './utils' to './utils.ts').
|
|
pub fn resolve_import(base_path: &Path, import_str: &str) -> Result<PathBuf> {
|
|
let mut path = base_path.parent().unwrap().join(import_str);
|
|
|
|
// Auto-append extensions if missing
|
|
if !path.exists() {
|
|
if path.with_extension("ts").exists() {
|
|
path.set_extension("ts");
|
|
} else if path.with_extension("js").exists() {
|
|
path.set_extension("js");
|
|
}
|
|
}
|
|
|
|
if !path.exists() {
|
|
return Err(anyhow!("Cannot resolve import '{}' from {:?}", import_str, base_path));
|
|
}
|
|
|
|
Ok(path.canonicalize()?)
|
|
}
|