mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
unify path normalization utility (#6865)
This commit is contained in:
parent
c4d9c6aa4b
commit
ad4af23aba
4 changed files with 43 additions and 73 deletions
41
cli/fs.rs
41
cli/fs.rs
|
@ -1,10 +1,10 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
pub use deno_core::normalize_path;
|
||||
use deno_core::ErrBox;
|
||||
use std::env::current_dir;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Write;
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
|
||||
use deno_core::ErrBox;
|
||||
use std::path::{Path, PathBuf};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
pub fn write_file<T: AsRef<[u8]>>(
|
||||
|
@ -46,41 +46,6 @@ pub fn write_file_2<T: AsRef<[u8]>>(
|
|||
file.write_all(data.as_ref())
|
||||
}
|
||||
|
||||
/// IMPORTANT: This method is duplicated in core/module_specifier.rs
|
||||
///
|
||||
/// Normalize all itermediate components of the path (ie. remove "./" and "../" components).
|
||||
/// Similar to `fs::canonicalize()` but doesn't resolve symlinks.
|
||||
///
|
||||
/// Taken from Cargo
|
||||
/// https://github.com/rust-lang/cargo/blob/af307a38c20a753ec60f0ad18be5abed3db3c9ac/src/cargo/util/paths.rs#L60-L85
|
||||
pub fn normalize_path(path: &Path) -> PathBuf {
|
||||
let mut components = path.components().peekable();
|
||||
let mut ret =
|
||||
if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
|
||||
components.next();
|
||||
PathBuf::from(c.as_os_str())
|
||||
} else {
|
||||
PathBuf::new()
|
||||
};
|
||||
|
||||
for component in components {
|
||||
match component {
|
||||
Component::Prefix(..) => unreachable!(),
|
||||
Component::RootDir => {
|
||||
ret.push(component.as_os_str());
|
||||
}
|
||||
Component::CurDir => {}
|
||||
Component::ParentDir => {
|
||||
ret.pop();
|
||||
}
|
||||
Component::Normal(c) => {
|
||||
ret.push(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
pub fn resolve_from_cwd(path: &Path) -> Result<PathBuf, ErrBox> {
|
||||
let resolved_path = if path.is_absolute() {
|
||||
path.to_owned()
|
||||
|
|
|
@ -15,6 +15,7 @@ mod es_isolate;
|
|||
mod flags;
|
||||
mod module_specifier;
|
||||
mod modules;
|
||||
mod normalize_path;
|
||||
mod ops;
|
||||
pub mod plugin_api;
|
||||
mod resources;
|
||||
|
@ -43,6 +44,7 @@ pub use crate::modules::ModuleLoader;
|
|||
pub use crate::modules::ModuleSource;
|
||||
pub use crate::modules::ModuleSourceFuture;
|
||||
pub use crate::modules::RecursiveModuleLoad;
|
||||
pub use crate::normalize_path::normalize_path;
|
||||
pub use crate::ops::Buf;
|
||||
pub use crate::ops::Op;
|
||||
pub use crate::ops::OpAsyncFuture;
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
use crate::normalize_path;
|
||||
use std::env::current_dir;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::path::Component;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use url::ParseError;
|
||||
use url::Url;
|
||||
|
@ -206,42 +205,10 @@ impl PartialEq<String> for ModuleSpecifier {
|
|||
}
|
||||
}
|
||||
|
||||
/// Normalize all itermediate components of the path (ie. remove "./" and "../" components).
|
||||
/// Similar to `fs::canonicalize()` but doesn't resolve symlinks.
|
||||
///
|
||||
/// Taken from Cargo
|
||||
/// https://github.com/rust-lang/cargo/blob/af307a38c20a753ec60f0ad18be5abed3db3c9ac/src/cargo/util/paths.rs#L60-L85
|
||||
pub fn normalize_path(path: &Path) -> PathBuf {
|
||||
let mut components = path.components().peekable();
|
||||
let mut ret =
|
||||
if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
|
||||
components.next();
|
||||
PathBuf::from(c.as_os_str())
|
||||
} else {
|
||||
PathBuf::new()
|
||||
};
|
||||
|
||||
for component in components {
|
||||
match component {
|
||||
Component::Prefix(..) => unreachable!(),
|
||||
Component::RootDir => {
|
||||
ret.push(component.as_os_str());
|
||||
}
|
||||
Component::CurDir => {}
|
||||
Component::ParentDir => {
|
||||
ret.pop();
|
||||
}
|
||||
Component::Normal(c) => {
|
||||
ret.push(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::path::Path;
|
||||
|
||||
#[test]
|
||||
fn test_resolve_import() {
|
||||
|
|
36
core/normalize_path.rs
Normal file
36
core/normalize_path.rs
Normal file
|
@ -0,0 +1,36 @@
|
|||
use std::path::Component;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Normalize all itermediate components of the path (ie. remove "./" and "../" components).
|
||||
/// Similar to `fs::canonicalize()` but doesn't resolve symlinks.
|
||||
///
|
||||
/// Taken from Cargo
|
||||
/// https://github.com/rust-lang/cargo/blob/af307a38c20a753ec60f0ad18be5abed3db3c9ac/src/cargo/util/paths.rs#L60-L85
|
||||
pub fn normalize_path(path: &Path) -> PathBuf {
|
||||
let mut components = path.components().peekable();
|
||||
let mut ret =
|
||||
if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
|
||||
components.next();
|
||||
PathBuf::from(c.as_os_str())
|
||||
} else {
|
||||
PathBuf::new()
|
||||
};
|
||||
|
||||
for component in components {
|
||||
match component {
|
||||
Component::Prefix(..) => unreachable!(),
|
||||
Component::RootDir => {
|
||||
ret.push(component.as_os_str());
|
||||
}
|
||||
Component::CurDir => {}
|
||||
Component::ParentDir => {
|
||||
ret.pop();
|
||||
}
|
||||
Component::Normal(c) => {
|
||||
ret.push(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
Loading…
Reference in a new issue