diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-08-16 10:42:11 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-16 10:42:11 -0400 |
commit | 4d1b263e91dc7b85e9c8cd1cb42270ddc0468396 (patch) | |
tree | 1bc685cd725652bcdaa0e35d88bf145f7a5d858a /cli/tools/registry/api.rs | |
parent | 57cd2951f193b02cbd3fb15f0ee14d83107a2855 (diff) |
feat(publish): suggest importing `jsr:@std/` for `deno.land/std` urls (#25046)
Diffstat (limited to 'cli/tools/registry/api.rs')
-rw-r--r-- | cli/tools/registry/api.rs | 62 |
1 files changed, 55 insertions, 7 deletions
diff --git a/cli/tools/registry/api.rs b/cli/tools/registry/api.rs index 519800660..c382aa9ac 100644 --- a/cli/tools/registry/api.rs +++ b/cli/tools/registry/api.rs @@ -149,14 +149,62 @@ pub async fn get_package( } pub fn get_jsr_alternative(imported: &Url) -> Option<String> { - if !matches!(imported.host_str(), Some("esm.sh")) { - return None; + if matches!(imported.host_str(), Some("esm.sh")) { + let mut segments = imported.path_segments()?; + match segments.next()? { + "gh" => None, + module => Some(format!("\"npm:{module}\"")), + } + } else if imported.as_str().starts_with("https://deno.land/") { + let mut segments = imported.path_segments()?; + let maybe_std = segments.next()?; + if maybe_std != "std" && !maybe_std.starts_with("std@") { + return None; + } + let module = segments.next()?; + let export = segments + .next() + .filter(|s| *s != "mod.ts") + .map(|s| s.strip_suffix(".ts").unwrap_or(s).replace("_", "-")); + Some(format!( + "\"jsr:@std/{}@1{}\"", + module, + export.map(|s| format!("/{}", s)).unwrap_or_default() + )) + } else { + None } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_jsr_alternative() { + #[track_caller] + fn run_test(imported: &str, output: Option<&str>) { + let imported = Url::parse(imported).unwrap(); + let output = output.map(|s| s.to_string()); + assert_eq!(get_jsr_alternative(&imported), output); + } - let mut segments = imported.path_segments().unwrap(); - match segments.next() { - Some("gh") => None, - Some(module) => Some(format!("\"npm:{module}\"")), - None => None, + run_test("https://esm.sh/ts-morph", Some("\"npm:ts-morph\"")); + run_test( + "https://deno.land/std/path/mod.ts", + Some("\"jsr:@std/path@1\""), + ); + run_test( + "https://deno.land/std/path/join.ts", + Some("\"jsr:@std/path@1/join\""), + ); + run_test( + "https://deno.land/std@0.229.0/path/join.ts", + Some("\"jsr:@std/path@1/join\""), + ); + run_test( + "https://deno.land/std@0.229.0/path/something_underscore.ts", + Some("\"jsr:@std/path@1/something-underscore\""), + ); } } |