lol
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

prefetch-npm-deps: add support for hosted git shorthands

+82 -6
+82 -6
pkgs/build-support/node/fetch-npm-deps/src/main.rs
··· 1 1 #![warn(clippy::pedantic)] 2 2 3 3 use crate::cacache::Cache; 4 - use anyhow::anyhow; 4 + use anyhow::{anyhow, Context}; 5 5 use rayon::prelude::*; 6 6 use serde::Deserialize; 7 7 use std::{ ··· 31 31 dependencies: Option<HashMap<String, OldPackage>>, 32 32 } 33 33 34 - #[derive(Deserialize)] 34 + #[derive(Debug, Deserialize, PartialEq, Eq)] 35 35 struct Package { 36 36 resolved: Option<UrlOrString>, 37 37 integrity: Option<String>, ··· 53 53 } 54 54 } 55 55 56 + #[allow(clippy::case_sensitive_file_extension_comparisons)] 56 57 fn to_new_packages( 57 58 old_packages: HashMap<String, OldPackage>, 59 + initial_url: &Url, 58 60 ) -> anyhow::Result<HashMap<String, Package>> { 59 61 let mut new = HashMap::new(); 60 62 61 - for (name, package) in old_packages { 63 + for (name, mut package) in old_packages { 64 + if let UrlOrString::Url(v) = &package.version { 65 + for (scheme, host) in [ 66 + ("github", "github.com"), 67 + ("bitbucket", "bitbucket.org"), 68 + ("gitlab", "gitlab.com"), 69 + ] { 70 + if v.scheme() == scheme { 71 + package.version = { 72 + let mut new_url = initial_url.clone(); 73 + 74 + new_url.set_host(Some(host))?; 75 + 76 + if v.path().ends_with(".git") { 77 + new_url.set_path(v.path()); 78 + } else { 79 + new_url.set_path(&format!("{}.git", v.path())); 80 + } 81 + 82 + new_url.set_fragment(v.fragment()); 83 + 84 + UrlOrString::Url(new_url) 85 + }; 86 + 87 + break; 88 + } 89 + } 90 + } 91 + 62 92 new.insert( 63 93 format!("{name}-{}", package.version), 64 94 Package { ··· 72 102 ); 73 103 74 104 if let Some(dependencies) = package.dependencies { 75 - new.extend(to_new_packages(dependencies)?); 105 + new.extend(to_new_packages(dependencies, initial_url)?); 76 106 } 77 107 } 78 108 ··· 200 230 } 201 231 } 202 232 233 + fn get_initial_url() -> anyhow::Result<Url> { 234 + Url::parse("git+ssh://git@a.b").context("initial url should be valid") 235 + } 236 + 203 237 fn main() -> anyhow::Result<()> { 204 238 let args = env::args().collect::<Vec<_>>(); 205 239 ··· 229 263 eprintln!("lockfile version: {}", lock.version); 230 264 231 265 let packages = match lock.version { 232 - 1 => lock.dependencies.map(to_new_packages).transpose()?, 266 + 1 => { 267 + let initial_url = get_initial_url()?; 268 + 269 + lock.dependencies 270 + .map(|p| to_new_packages(p, &initial_url)) 271 + .transpose()? 272 + } 233 273 2 | 3 => lock.packages, 234 274 _ => panic!( 235 275 "We don't support lockfile version {}, please file an issue.", ··· 297 337 298 338 #[cfg(test)] 299 339 mod tests { 300 - use super::{get_hosted_git_url, get_ideal_hash}; 340 + use super::{ 341 + get_hosted_git_url, get_ideal_hash, get_initial_url, to_new_packages, OldPackage, Package, 342 + UrlOrString, 343 + }; 344 + use std::collections::HashMap; 301 345 use url::Url; 302 346 303 347 #[test] ··· 347 391 ] { 348 392 assert_eq!(get_ideal_hash(input).ok(), expected); 349 393 } 394 + } 395 + 396 + #[test] 397 + fn git_shorthand_v1() -> anyhow::Result<()> { 398 + let old = 399 + { 400 + let mut o = HashMap::new(); 401 + o.insert( 402 + String::from("sqlite3"), 403 + OldPackage { 404 + version: UrlOrString::Url(Url::parse( 405 + "github:mapbox/node-sqlite3#593c9d498be2510d286349134537e3bf89401c4a", 406 + ).unwrap()), 407 + resolved: None, 408 + integrity: None, 409 + dependencies: None, 410 + }, 411 + ); 412 + o 413 + }; 414 + 415 + let initial_url = get_initial_url()?; 416 + 417 + let new = to_new_packages(old, &initial_url)?; 418 + 419 + assert_eq!(new.len(), 1, "new packages map should contain 1 value"); 420 + assert_eq!(new.into_values().next().unwrap(), Package { 421 + resolved: Some(UrlOrString::Url(Url::parse("git+ssh://git@github.com/mapbox/node-sqlite3.git#593c9d498be2510d286349134537e3bf89401c4a").unwrap())), 422 + integrity: None 423 + }); 424 + 425 + Ok(()) 350 426 } 351 427 }