diff options
Diffstat (limited to 'cli/args/mod.rs')
-rw-r--r-- | cli/args/mod.rs | 74 |
1 files changed, 49 insertions, 25 deletions
diff --git a/cli/args/mod.rs b/cli/args/mod.rs index e6ada9453..d91affed1 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -7,7 +7,6 @@ mod lockfile; pub mod package_json; pub use self::import_map::resolve_import_map_from_specifier; -pub use self::lockfile::snapshot_from_lockfile; use self::package_json::PackageJsonDeps; use ::import_map::ImportMap; use deno_core::resolve_url_or_path; @@ -55,6 +54,8 @@ use deno_runtime::inspector_server::InspectorServer; use deno_runtime::permissions::PermissionsOptions; use once_cell::sync::Lazy; use once_cell::sync::OnceCell; +use serde::Deserialize; +use serde::Serialize; use std::collections::HashMap; use std::env; use std::io::BufReader; @@ -67,8 +68,6 @@ use std::sync::Arc; use thiserror::Error; use crate::file_fetcher::FileFetcher; -use crate::npm::CliNpmRegistryApi; -use crate::npm::NpmProcessState; use crate::util::fs::canonicalize_path_maybe_not_exists; use crate::util::glob::expand_globs; use crate::version; @@ -77,6 +76,28 @@ use deno_config::FmtConfig; use deno_config::LintConfig; use deno_config::TestConfig; +static NPM_REGISTRY_DEFAULT_URL: Lazy<Url> = Lazy::new(|| { + let env_var_name = "NPM_CONFIG_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) => { + return url; + } + Err(err) => { + log::debug!("Invalid {} environment variable: {:#}", env_var_name, err,); + } + } + } + + Url::parse("https://registry.npmjs.org").unwrap() +}); + +pub fn npm_registry_default_url() -> &'static Url { + &NPM_REGISTRY_DEFAULT_URL +} + pub fn ts_config_to_emit_options( config: deno_config::TsConfig, ) -> deno_ast::EmitOptions { @@ -545,6 +566,13 @@ pub fn get_root_cert_store( Ok(root_cert_store) } +/// State provided to the process via an environment variable. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct NpmProcessState { + pub snapshot: deno_npm::resolution::SerializedNpmResolutionSnapshot, + pub local_node_modules_path: Option<String>, +} + const RESOLUTION_STATE_ENV_VAR_NAME: &str = "DENO_DONT_USE_INTERNAL_NODE_COMPAT_STATE"; @@ -560,7 +588,7 @@ static NPM_PROCESS_STATE: Lazy<Option<NpmProcessState>> = Lazy::new(|| { /// Overrides for the options below that when set will /// use these values over the values derived from the /// CLI flags or config file. -#[derive(Default)] +#[derive(Default, Clone)] struct CliOptionOverrides { import_map_specifier: Option<Option<ModuleSpecifier>>, } @@ -843,32 +871,15 @@ impl CliOptions { } } - pub async fn resolve_npm_resolution_snapshot( + pub fn resolve_npm_resolution_snapshot( &self, - api: &CliNpmRegistryApi, ) -> Result<Option<ValidSerializedNpmResolutionSnapshot>, AnyError> { if let Some(state) = &*NPM_PROCESS_STATE { // TODO(bartlomieju): remove this clone - return Ok(Some(state.snapshot.clone().into_valid()?)); - } - - if let Some(lockfile) = self.maybe_lockfile() { - if !lockfile.lock().overwrite { - let snapshot = snapshot_from_lockfile(lockfile.clone(), api) - .await - .with_context(|| { - format!( - "failed reading lockfile '{}'", - lockfile.lock().filename.display() - ) - })?; - // clear the memory cache to reduce memory usage - api.clear_memory_cache(); - return Ok(Some(snapshot)); - } + Ok(Some(state.snapshot.clone().into_valid()?)) + } else { + Ok(None) } - - Ok(None) } // If the main module should be treated as being in an npm package. @@ -892,6 +903,19 @@ impl CliOptions { self.maybe_node_modules_folder.clone() } + pub fn with_node_modules_dir_path(&self, path: PathBuf) -> Self { + Self { + flags: self.flags.clone(), + initial_cwd: self.initial_cwd.clone(), + maybe_node_modules_folder: Some(path), + maybe_vendor_folder: self.maybe_vendor_folder.clone(), + maybe_config_file: self.maybe_config_file.clone(), + maybe_package_json: self.maybe_package_json.clone(), + maybe_lockfile: self.maybe_lockfile.clone(), + overrides: self.overrides.clone(), + } + } + pub fn node_modules_dir_enablement(&self) -> Option<bool> { self.flags.node_modules_dir.or_else(|| { self |