summaryrefslogtreecommitdiff
path: root/cli/fs.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2019-07-18 00:15:30 +0200
committerRyan Dahl <ry@tinyclouds.org>2019-07-17 18:15:30 -0400
commit8214b686cea3f6ad57d7da49a44d33185fdeb098 (patch)
tree00517c7b8f4bb835ce050e89f29ec1826bac92ce /cli/fs.rs
parent481a82c983e40201589e105e28be4ce809e46a60 (diff)
Refactor DenoDir (#2636)
* rename `ModuleMetaData` to `SourceFile` and remove TS specific functionality * add `TsCompiler` struct encapsulating processing of TypeScript files * move `SourceMapGetter` trait implementation to `//cli/compiler.rs` * add low-level `DiskCache` API for general purpose caches and use it in `DenoDir` and `TsCompiler` for filesystem access * don't use hash-like filenames for compiled modules, instead use metadata file for storing compilation hash * add `SourceFileCache` for in-process caching of loaded files for fast subsequent access * define `SourceFileFetcher` trait encapsulating loading of local and remote files and implement it for `DenoDir` * define `use_cache` and `no_fetch` flags on `DenoDir` instead of using in fetch methods
Diffstat (limited to 'cli/fs.rs')
-rw-r--r--cli/fs.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/cli/fs.rs b/cli/fs.rs
index 6fe891141..34e4d59f2 100644
--- a/cli/fs.rs
+++ b/cli/fs.rs
@@ -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))
+}