From f8bcf6be28a1aab9e3be45755aac644fc65db681 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 7 Dec 2022 16:10:50 -0500 Subject: feat(npm): add support for `NPM_CONFIG_REGISTRY` (#16980) --- cli/args/flags.rs | 1 + cli/npm/registry.rs | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 13 deletions(-) (limited to 'cli') diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 7a4416cf5..f1acbf48d 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -547,6 +547,7 @@ static ENV_VARIABLES_HELP: &str = r#"ENVIRONMENT VARIABLES: (module downloads, fetch) HTTPS_PROXY Proxy address for HTTPS requests (module downloads, fetch) + NPM_CONFIG_REGISTRY URL to use for the npm registry. NO_COLOR Set to disable color NO_PROXY Comma-separated list of hosts which do not use a proxy (module downloads, fetch)"#; diff --git a/cli/npm/registry.rs b/cli/npm/registry.rs index 9ba565366..749a047ab 100644 --- a/cli/npm/registry.rs +++ b/cli/npm/registry.rs @@ -228,22 +228,37 @@ pub struct RealNpmRegistryApi(Arc); impl RealNpmRegistryApi { pub fn default_url() -> Url { - let env_var_name = "DENO_NPM_REGISTRY"; - if let Ok(registry_url) = std::env::var(env_var_name) { - // ensure there is a trailing slash for the directory - let registry_url = format!("{}/", registry_url.trim_end_matches('/')); - match Url::parse(®istry_url) { - Ok(url) => url, - Err(err) => { - eprintln!("{}: Invalid {} environment variable. Please provide a valid url.\n\n{:#}", - colors::red_bold("error"), - env_var_name, err); - std::process::exit(1); + // todo(dsherret): remove DENO_NPM_REGISTRY in the future (maybe May 2023) + let env_var_names = ["NPM_CONFIG_REGISTRY", "DENO_NPM_REGISTRY"]; + for env_var_name in env_var_names { + if let Ok(registry_url) = std::env::var(env_var_name) { + // ensure there is a trailing slash for the directory + let registry_url = format!("{}/", registry_url.trim_end_matches('/')); + match Url::parse(®istry_url) { + Ok(url) => { + if env_var_name == "DENO_NPM_REGISTRY" { + log::warn!( + "{}", + colors::yellow(concat!( + "DENO_NPM_REGISTRY was intended for internal testing purposes only. ", + "Please update to NPM_CONFIG_REGISTRY instead.", + )), + ); + } + return url; + } + Err(err) => { + log::debug!( + "Invalid {} environment variable: {:#}", + env_var_name, + err, + ); + } } } - } else { - Url::parse("https://registry.npmjs.org").unwrap() } + + Url::parse("https://registry.npmjs.org").unwrap() } pub fn new( -- cgit v1.2.3