summaryrefslogtreecommitdiff
path: root/cli/npm/resolvers/local.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2023-04-14 16:22:33 -0400
committerGitHub <noreply@github.com>2023-04-14 16:22:33 -0400
commit136dce67cec749dce5989ea29e88359ef79a0045 (patch)
tree38e96bbbf22dc06cdba418a35467b215f1335549 /cli/npm/resolvers/local.rs
parenta4111442191fff300132259752e6d2d5613d1871 (diff)
refactor: break up `ProcState` (#18707)
1. Breaks up functionality within `ProcState` into several other structs to break out the responsibilities (`ProcState` is only a data struct now). 2. Moves towards being able to inject dependencies more easily and have functionality only require what it needs. 3. Exposes `Arc<T>` around the "service structs" instead of it being embedded within them. The idea behind embedding them was to reduce the verbosity of needing to pass around `Arc<...>`, but I don't think it was exactly working and as we move more of these structs to be more injectable I don't think the extra verbosity will be a big deal.
Diffstat (limited to 'cli/npm/resolvers/local.rs')
-rw-r--r--cli/npm/resolvers/local.rs53
1 files changed, 19 insertions, 34 deletions
diff --git a/cli/npm/resolvers/local.rs b/cli/npm/resolvers/local.rs
index 59d8b0829..d4085f345 100644
--- a/cli/npm/resolvers/local.rs
+++ b/cli/npm/resolvers/local.rs
@@ -8,6 +8,7 @@ use std::collections::VecDeque;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
+use std::sync::Arc;
use crate::util::fs::symlink_dir;
use crate::util::fs::LaxSingleProcessFsFlag;
@@ -41,11 +42,11 @@ use super::common::NpmPackageFsResolver;
/// Resolver that creates a local node_modules directory
/// and resolves packages from it.
-#[derive(Debug, Clone)]
+#[derive(Debug)]
pub struct LocalNpmPackageResolver {
- cache: NpmCache,
+ cache: Arc<NpmCache>,
progress_bar: ProgressBar,
- resolution: NpmResolution,
+ resolution: Arc<NpmResolution>,
registry_url: Url,
root_node_modules_path: PathBuf,
root_node_modules_url: Url,
@@ -53,11 +54,11 @@ pub struct LocalNpmPackageResolver {
impl LocalNpmPackageResolver {
pub fn new(
- cache: NpmCache,
+ cache: Arc<NpmCache>,
progress_bar: ProgressBar,
registry_url: Url,
node_modules_folder: PathBuf,
- resolution: NpmResolution,
+ resolution: Arc<NpmResolution>,
) -> Self {
Self {
cache,
@@ -103,11 +104,19 @@ impl LocalNpmPackageResolver {
// it's within the directory, so use it
specifier.to_file_path().ok()
}
+}
- fn get_package_id_folder(
- &self,
- id: &NpmPackageId,
- ) -> Result<PathBuf, AnyError> {
+#[async_trait]
+impl NpmPackageFsResolver for LocalNpmPackageResolver {
+ fn root_dir_url(&self) -> &Url {
+ &self.root_node_modules_url
+ }
+
+ fn node_modules_path(&self) -> Option<PathBuf> {
+ Some(self.root_node_modules_path.clone())
+ }
+
+ fn package_folder(&self, id: &NpmPackageId) -> Result<PathBuf, AnyError> {
match self.resolution.resolve_package_cache_folder_id_from_id(id) {
// package is stored at:
// node_modules/.deno/<package_cache_folder_id_folder_name>/node_modules/<package_name>
@@ -125,24 +134,6 @@ impl LocalNpmPackageResolver {
),
}
}
-}
-
-#[async_trait]
-impl NpmPackageFsResolver for LocalNpmPackageResolver {
- fn root_dir_url(&self) -> &Url {
- &self.root_node_modules_url
- }
-
- fn node_modules_path(&self) -> Option<PathBuf> {
- Some(self.root_node_modules_path.clone())
- }
-
- fn resolve_package_folder_from_deno_module(
- &self,
- node_id: &NpmPackageId,
- ) -> Result<PathBuf, AnyError> {
- self.get_package_id_folder(node_id)
- }
fn resolve_package_folder_from_package(
&self,
@@ -198,12 +189,6 @@ impl NpmPackageFsResolver for LocalNpmPackageResolver {
Ok(package_root_path)
}
- fn package_size(&self, id: &NpmPackageId) -> Result<u64, AnyError> {
- let package_folder_path = self.get_package_id_folder(id)?;
-
- Ok(crate::util::fs::dir_size(&package_folder_path)?)
- }
-
async fn cache_packages(&self) -> Result<(), AnyError> {
sync_resolution_with_fs(
&self.resolution.snapshot(),
@@ -231,7 +216,7 @@ impl NpmPackageFsResolver for LocalNpmPackageResolver {
/// Creates a pnpm style folder structure.
async fn sync_resolution_with_fs(
snapshot: &NpmResolutionSnapshot,
- cache: &NpmCache,
+ cache: &Arc<NpmCache>,
progress_bar: &ProgressBar,
registry_url: &Url,
root_node_modules_dir_path: &Path,