diff options
Diffstat (limited to 'core/module_specifier.rs')
-rw-r--r-- | core/module_specifier.rs | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/core/module_specifier.rs b/core/module_specifier.rs index 72d693723..6c6dbad95 100644 --- a/core/module_specifier.rs +++ b/core/module_specifier.rs @@ -123,7 +123,7 @@ pub fn resolve_url( /// e.g. 'http:' or 'file:' or 'git+ssh:'. If not, it's interpreted as a /// file path; if it is a relative path it's resolved relative to the current /// working directory. -pub fn resolve_url_or_path( +pub fn resolve_url_or_path_deprecated( specifier: &str, ) -> Result<ModuleSpecifier, ModuleResolutionError> { if specifier_has_uri_scheme(specifier) { @@ -135,9 +135,26 @@ pub fn resolve_url_or_path( } } +/// Takes a string representing either an absolute URL or a file path, +/// as it may be passed to deno as a command line argument. +/// The string is interpreted as a URL if it starts with a valid URI scheme, +/// e.g. 'http:' or 'file:' or 'git+ssh:'. If not, it's interpreted as a +/// file path; if it is a relative path it's resolved relative to passed +/// `current_dir`. +pub fn resolve_url_or_path( + specifier: &str, + current_dir: &Path, +) -> Result<ModuleSpecifier, ModuleResolutionError> { + if specifier_has_uri_scheme(specifier) { + resolve_url(specifier) + } else { + resolve_path(specifier, current_dir) + } +} + /// Converts a string representing a relative or absolute path into a -/// ModuleSpecifier. A relative path is considered relative to the current -/// working directory. +/// ModuleSpecifier. A relative path is considered relative to the passed +/// `current_dir`. pub fn resolve_path( path_str: &str, current_dir: &Path, @@ -344,7 +361,7 @@ mod tests { } #[test] - fn test_resolve_url_or_path() { + fn test_resolve_url_or_path_deprecated() { // Absolute URL. let mut tests: Vec<(&str, String)> = vec![ ( @@ -440,13 +457,15 @@ mod tests { } for (specifier, expected_url) in tests { - let url = resolve_url_or_path(specifier).unwrap().to_string(); + let url = resolve_url_or_path_deprecated(specifier) + .unwrap() + .to_string(); assert_eq!(url, expected_url); } } #[test] - fn test_resolve_url_or_path_error() { + fn test_resolve_url_or_path_deprecated_error() { use url::ParseError::*; use ModuleResolutionError::*; @@ -460,7 +479,7 @@ mod tests { } for (specifier, expected_err) in tests { - let err = resolve_url_or_path(specifier).unwrap_err(); + let err = resolve_url_or_path_deprecated(specifier).unwrap_err(); assert_eq!(err, expected_err); } } |