diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-09-22 11:17:02 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-22 11:17:02 -0400 |
commit | 716005a0d4afd1042fa75d8bdc32fd13e9ebe95f (patch) | |
tree | 4c417eb7b91d6203aacba7dcd81bee3f13c0cfd3 /ext/node/path.rs | |
parent | 9a216806d514b5f41c73c777010572cdf3c51eab (diff) |
feat(npm): add flag for creating and resolving npm packages to a local node_modules folder (#15971)
Diffstat (limited to 'ext/node/path.rs')
-rw-r--r-- | ext/node/path.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/ext/node/path.rs b/ext/node/path.rs new file mode 100644 index 000000000..8477fe713 --- /dev/null +++ b/ext/node/path.rs @@ -0,0 +1,38 @@ +use std::path::Component; +use std::path::PathBuf; + +/// Extenion to path_clean::PathClean +pub trait PathClean<T> { + fn clean(&self) -> T; +} + +impl PathClean<PathBuf> for PathBuf { + fn clean(&self) -> PathBuf { + let path = path_clean::PathClean::clean(self); + if cfg!(windows) && path.to_string_lossy().contains("..\\") { + // temporary workaround because path_clean::PathClean::clean is + // not good enough on windows + let mut components = Vec::new(); + + for component in path.components() { + match component { + Component::CurDir => { + // skip + } + Component::ParentDir => { + let poped_component = components.pop(); + if !matches!(poped_component, Some(Component::Normal(_))) { + panic!("Error normalizing: {}", path.display()); + } + } + Component::Normal(_) | Component::RootDir | Component::Prefix(_) => { + components.push(component); + } + } + } + components.into_iter().collect::<PathBuf>() + } else { + path + } + } +} |