diff options
Diffstat (limited to 'cli/fs.rs')
-rw-r--r-- | cli/fs.rs | 29 |
1 files changed, 29 insertions, 0 deletions
@@ -8,6 +8,7 @@ use std::path::{Path, PathBuf}; use deno::ErrBox; use rand; use rand::Rng; +use url::Url; #[cfg(unix)] use nix::unistd::{chown as unix_chown, Gid, Uid}; @@ -126,3 +127,31 @@ pub fn chown(_path: &str, _uid: u32, _gid: u32) -> Result<(), ErrBox> { // TODO: implement chown for Windows Err(crate::deno_error::op_not_implemented()) } + +pub fn resolve_from_cwd(path: &str) -> Result<(PathBuf, String), ErrBox> { + let candidate_path = Path::new(path); + + let resolved_path = if candidate_path.is_absolute() { + candidate_path.to_owned() + } else { + let cwd = std::env::current_dir().unwrap(); + cwd.join(path) + }; + + // HACK: `Url::from_directory_path` is used here because it normalizes the path. + // Joining `/dev/deno/" with "./tests" using `PathBuf` yields `/deno/dev/./tests/`. + // On the other hand joining `/dev/deno/" with "./tests" using `Url` yields "/dev/deno/tests" + // - and that's what we want. + // There exists similar method on `PathBuf` - `PathBuf.canonicalize`, but the problem + // is `canonicalize` resolves symlinks and we don't want that. + // We just want o normalize the path... + let resolved_url = Url::from_file_path(resolved_path) + .expect("PathBuf should be parseable URL"); + let normalized_path = resolved_url + .to_file_path() + .expect("URL from PathBuf should be valid path"); + + let path_string = normalized_path.to_str().unwrap().to_string(); + + Ok((normalized_path, path_string)) +} |