Skip to content

Commit b16a656

Browse files
committed
Updates switch to dowload berry from npm
1 parent a7832b7 commit b16a656

3 files changed

Lines changed: 42 additions & 17 deletions

File tree

packages/zpm-switch/src/cache.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ use std::{future::Future, io::Write};
22

33
use serde::{Deserialize, Serialize};
44
use zpm_parsers::JsonDocument;
5-
use zpm_semver::{Version, VersionRc};
6-
use zpm_utils::{DataType, Hash64, Path, ToFileString, ToHumanString, Unit, is_terminal};
7-
use zpm_utils::eco_vec;
5+
use zpm_semver::{Range, VersionRc};
6+
use zpm_utils::{DataType, FromFileString, Hash64, Path, ToFileString, ToHumanString, Unit, is_terminal};
87

98
use crate::errors::Error;
109

11-
pub const CACHE_VERSION: usize = 1;
10+
pub const CACHE_VERSION: usize = 2;
1211

1312
#[derive(Serialize, Deserialize)]
1413
#[serde(rename_all = "camelCase")]
@@ -26,18 +25,17 @@ fn get_npm_registry_server() -> String {
2625
impl CacheKey {
2726
pub fn to_npm_url(&self) -> Option<String> {
2827
if self.version.rc.as_ref().map_or(true, |rc| !rc.starts_with(&[VersionRc::String("git".into())])) {
29-
// Older RC versions (<6.0.0-rc.9) are not available in npm
30-
let first_npm_release = Version::new_from_components(
31-
6,
32-
0,
33-
0,
34-
Some(eco_vec![VersionRc::String("rc".into()), VersionRc::Number(9)]),
35-
);
36-
37-
if self.version >= first_npm_release {
28+
// zpm is available on npm since 6.0.0-rc.9
29+
if Range::from_file_string(">=6.0.0-rc.9").unwrap().check_ignore_rc(&self.version) {
3830
let registry = get_npm_registry_server();
3931
return Some(format!("{}/@yarnpkg/yarn-{}/-/yarn-{}-{}.tgz", registry, self.platform, self.platform, self.version.to_file_string()));
4032
}
33+
34+
// berry has been published to npm since 2.4.1
35+
if Range::from_file_string(">=2.4.1 <6.0.0-0").unwrap().check_ignore_rc(&self.version) {
36+
let registry = get_npm_registry_server();
37+
return Some(format!("{}/@yarnpkg/cli-bin/-/cli-bin-{}.tgz", registry, self.version.to_file_string()));
38+
}
4139
}
4240

4341
None

packages/zpm-switch/src/install.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ async fn install_node_js_from_url(source: &cache::CacheKey) -> Result<Command, E
119119
Ok(command)
120120
}
121121

122-
async fn install_node_js_from_package(source: &cache::CacheKey, main_file: &Path) -> Result<Command, Error> {
122+
async fn install_node_js_from_package(source: &cache::CacheKey, url: &str, main_file: &Path) -> Result<Command, Error> {
123123
let cache_path = cache::ensure(source, |p| async move {
124124
let compressed_data
125-
= fetch(&source.to_url()).await?;
125+
= fetch(url).await?;
126126

127127
tokio::task::spawn_blocking(move || -> Result<(), Error> {
128128
let data
@@ -153,6 +153,18 @@ async fn install_node_js_from_package(source: &cache::CacheKey, main_file: &Path
153153
Ok(command)
154154
}
155155

156+
async fn install_berry(source: &cache:: CacheKey) -> Result<Command, Error> {
157+
if let Some(npm_url) = source.to_npm_url() {
158+
install_node_js_from_package(source, &npm_url, &Path::from_str("bin/yarn.js").unwrap()).await
159+
} else {
160+
install_node_js_from_url(source).await
161+
}
162+
}
163+
164+
async fn install_yarnpkg_legacy(source: &cache:: CacheKey) -> Result<Command, Error> {
165+
install_node_js_from_package(source, &source.to_url(), &Path::from_str("bin/yarn.js").unwrap()).await
166+
}
167+
156168
pub async fn install_package_manager(package_manager: &VersionPackageManagerReference) -> Result<Command, Error> {
157169
let version_platform = cache::CacheKey {
158170
cache_version: cache::CACHE_VERSION,
@@ -165,11 +177,11 @@ pub async fn install_package_manager(package_manager: &VersionPackageManagerRefe
165177
}
166178

167179
if zpm_semver::Range::from_file_string(">=2.0.0-0").unwrap().check_ignore_rc(&package_manager.version) {
168-
return install_node_js_from_url(&version_platform).await;
180+
return install_berry(&version_platform).await;
169181
}
170182

171183
if zpm_semver::Range::from_file_string(">=0.0.0-0").unwrap().check_ignore_rc(&package_manager.version) {
172-
return install_node_js_from_package(&version_platform, &Path::from_str("bin/yarn.js").unwrap()).await;
184+
return install_yarnpkg_legacy(&version_platform).await;
173185
}
174186

175187
unreachable!()

packages/zpm/src/http_npm.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ macro_rules! scope_registry_setting {
6262
}
6363
}
6464

65+
let host = url::Url::parse($registry).ok()?.origin().ascii_serialization();
66+
if let Some(host_settings) = $config.settings.npm_hosts.get(&host) {
67+
if let Some(value) = host_settings.$field.value.as_ref() {
68+
return Some(value);
69+
}
70+
}
71+
72+
// Also try with trailing slash (for normalization)
73+
let host_with_slash = format!("{}/", host);
74+
if let Some(host_settings) = $config.settings.npm_hosts.get(&host_with_slash) {
75+
if let Some(value) = host_settings.$field.value.as_ref() {
76+
return Some(value);
77+
}
78+
}
79+
6580
None
6681
})()
6782
}

0 commit comments

Comments
 (0)