diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-08-10 15:23:58 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-10 15:23:58 -0400 |
commit | d9fae38d1e093fd2578c096203f1bddc18aa8ddb (patch) | |
tree | b3dc3e4f442ffc0b1461f30a3435388a7b3f4b87 /cli/fs_util.rs | |
parent | d0ffa0beb52679ddfc90ccc03e27572337db79dc (diff) |
feat: add initial internal npm client and dependency resolver (#15446)
Diffstat (limited to 'cli/fs_util.rs')
-rw-r--r-- | cli/fs_util.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/cli/fs_util.rs b/cli/fs_util.rs index 322b3f9e7..69d4f7248 100644 --- a/cli/fs_util.rs +++ b/cli/fs_util.rs @@ -447,6 +447,48 @@ pub fn path_with_stem_suffix(path: &Path, suffix: &str) -> PathBuf { } } +/// Gets if the provided character is not supported on all +/// kinds of file systems. +pub fn is_banned_path_char(c: char) -> bool { + matches!(c, '<' | '>' | ':' | '"' | '|' | '?' | '*') +} + +/// Gets a safe local directory name for the provided url. +/// +/// For example: +/// https://deno.land:8080/path -> deno.land_8080/path +pub fn root_url_to_safe_local_dirname(root: &ModuleSpecifier) -> PathBuf { + fn sanitize_segment(text: &str) -> String { + text + .chars() + .map(|c| if is_banned_segment_char(c) { '_' } else { c }) + .collect() + } + + fn is_banned_segment_char(c: char) -> bool { + matches!(c, '/' | '\\') || is_banned_path_char(c) + } + + let mut result = String::new(); + if let Some(domain) = root.domain() { + result.push_str(&sanitize_segment(domain)); + } + if let Some(port) = root.port() { + if !result.is_empty() { + result.push('_'); + } + result.push_str(&port.to_string()); + } + let mut result = PathBuf::from(result); + if let Some(segments) = root.path_segments() { + for segment in segments.filter(|s| !s.is_empty()) { + result = result.join(sanitize_segment(segment)); + } + } + + result +} + #[cfg(test)] mod tests { use super::*; |