Skip to main content

stylex_path_resolver/file_system/
mod.rs

1use std::{
2  fs,
3  path::{Path, PathBuf},
4};
5
6pub(crate) fn _check_directory(path: &Path) -> bool {
7  match fs::metadata(path) {
8    Ok(metadata) => metadata.is_dir(),
9    Err(_) => false,
10  }
11}
12
13pub(crate) fn _get_directories(path: &Path) -> Result<Vec<PathBuf>, std::io::Error> {
14  Ok(
15    fs::read_dir(path)?
16      .filter_map(|entry| {
17        entry.ok().and_then(|e| {
18          let path = e.path();
19          if path.is_dir() { Some(path) } else { None }
20        })
21      })
22      .collect::<Vec<_>>(),
23  )
24}
25
26pub(crate) fn _get_directory_path_recursive(path: &Path) -> Option<PathBuf> {
27  if path.as_os_str().is_empty() {
28    return None;
29  }
30
31  if _check_directory(path) {
32    return Some(path.to_path_buf());
33  }
34
35  match path.parent() {
36    Some(parent) => _get_directory_path_recursive(parent),
37    None => None,
38  }
39}
40
41pub(crate) fn find_closest_path(path: &Path, target_folder_name: &str) -> Option<PathBuf> {
42  let node_modules_path: PathBuf = path.join(target_folder_name);
43
44  if node_modules_path.exists() {
45    return Some(node_modules_path);
46  }
47
48  match path.parent() {
49    Some(parent) => find_closest_path(parent, target_folder_name),
50    None => None,
51  }
52}