From bcb6ee9d0864f490f6da47cbe2593310b21333ff Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sun, 12 Mar 2023 23:32:59 -0400 Subject: refactor(npm): push npm struct creation to a higher level (#18139) This has been bothering me for a while and it became more painful while working on #18136 because injecting the shared progress bar became very verbose. Basically we should move the creation of all these npm structs up to a higher level. This is a stepping stone for a future refactor where we can improve how we create all our structs. --- cli/args/mod.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'cli/args') diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 71cc4e218..5be5fc7ab 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -12,6 +12,7 @@ use self::package_json::PackageJsonDeps; use ::import_map::ImportMap; use indexmap::IndexMap; +use crate::npm::NpmRegistryApi; use crate::npm::NpmResolutionSnapshot; pub use config_file::BenchConfig; pub use config_file::CompilerOptions; @@ -664,13 +665,31 @@ impl CliOptions { .map(Some) } - pub fn get_npm_resolution_snapshot(&self) -> Option { + pub async fn resolve_npm_resolution_snapshot( + &self, + api: &NpmRegistryApi, + ) -> Result, AnyError> { if let Some(state) = &*NPM_PROCESS_STATE { // TODO(bartlomieju): remove this clone - return Some(state.snapshot.clone()); + return Ok(Some(state.snapshot.clone())); + } + + if let Some(lockfile) = self.maybe_lock_file() { + if !lockfile.lock().overwrite { + return Ok(Some( + NpmResolutionSnapshot::from_lockfile(lockfile.clone(), api) + .await + .with_context(|| { + format!( + "failed reading lockfile '{}'", + lockfile.lock().filename.display() + ) + })?, + )); + } } - None + Ok(None) } // If the main module should be treated as being in an npm package. -- cgit v1.2.3