mirror of
https://github.com/denoland/deno
synced 2024-08-27 12:00:17 +00:00
chore: slight cleanup in npm resolvers (#20692)
This commit is contained in:
parent
50a3209fff
commit
dcb00bb9b8
|
@ -21,6 +21,7 @@ use deno_core::serde_json;
|
||||||
use deno_core::serde_json::json;
|
use deno_core::serde_json::json;
|
||||||
use deno_core::ModuleSpecifier;
|
use deno_core::ModuleSpecifier;
|
||||||
use deno_lint::rules::LintRule;
|
use deno_lint::rules::LintRule;
|
||||||
|
use deno_runtime::deno_node::NpmResolver;
|
||||||
use deno_runtime::deno_node::PackageJson;
|
use deno_runtime::deno_node::PackageJson;
|
||||||
use deno_runtime::deno_node::PathClean;
|
use deno_runtime::deno_node::PathClean;
|
||||||
use deno_semver::package::PackageReq;
|
use deno_semver::package::PackageReq;
|
||||||
|
|
|
@ -38,6 +38,7 @@ use deno_graph::ResolutionError;
|
||||||
use deno_graph::SpecifierError;
|
use deno_graph::SpecifierError;
|
||||||
use deno_lint::rules::LintRule;
|
use deno_lint::rules::LintRule;
|
||||||
use deno_runtime::deno_node;
|
use deno_runtime::deno_node;
|
||||||
|
use deno_runtime::deno_node::NpmResolver;
|
||||||
use deno_runtime::tokio_util::create_basic_runtime;
|
use deno_runtime::tokio_util::create_basic_runtime;
|
||||||
use deno_semver::npm::NpmPackageReqReference;
|
use deno_semver::npm::NpmPackageReqReference;
|
||||||
use deno_semver::package::PackageReq;
|
use deno_semver::package::PackageReq;
|
||||||
|
|
|
@ -49,6 +49,7 @@ use deno_core::JsRuntime;
|
||||||
use deno_core::ModuleSpecifier;
|
use deno_core::ModuleSpecifier;
|
||||||
use deno_core::OpState;
|
use deno_core::OpState;
|
||||||
use deno_core::RuntimeOptions;
|
use deno_core::RuntimeOptions;
|
||||||
|
use deno_runtime::deno_node::NpmResolver;
|
||||||
use deno_runtime::tokio_util::create_basic_runtime;
|
use deno_runtime::tokio_util::create_basic_runtime;
|
||||||
use lazy_regex::lazy_regex;
|
use lazy_regex::lazy_regex;
|
||||||
use log::error;
|
use log::error;
|
||||||
|
|
|
@ -10,7 +10,6 @@ use std::path::PathBuf;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use deno_ast::ModuleSpecifier;
|
use deno_ast::ModuleSpecifier;
|
||||||
use deno_core::anyhow::bail;
|
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::parking_lot::Mutex;
|
use deno_core::parking_lot::Mutex;
|
||||||
use deno_core::serde_json;
|
use deno_core::serde_json;
|
||||||
|
@ -24,7 +23,6 @@ use deno_runtime::deno_fs::FileSystem;
|
||||||
use deno_runtime::deno_node::NodePermissions;
|
use deno_runtime::deno_node::NodePermissions;
|
||||||
use deno_runtime::deno_node::NodeResolutionMode;
|
use deno_runtime::deno_node::NodeResolutionMode;
|
||||||
use deno_runtime::deno_node::NpmResolver;
|
use deno_runtime::deno_node::NpmResolver;
|
||||||
use deno_runtime::deno_node::PathClean;
|
|
||||||
use deno_semver::package::PackageNv;
|
use deno_semver::package::PackageNv;
|
||||||
use deno_semver::package::PackageReq;
|
use deno_semver::package::PackageReq;
|
||||||
use global::GlobalNpmPackageResolver;
|
use global::GlobalNpmPackageResolver;
|
||||||
|
@ -174,13 +172,6 @@ impl CliNpmResolver {
|
||||||
Ok(crate::util::fs::dir_size(&package_folder)?)
|
Ok(crate::util::fs::dir_size(&package_folder)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets if the provided specifier is in an npm package.
|
|
||||||
pub fn in_npm_package(&self, specifier: &ModuleSpecifier) -> bool {
|
|
||||||
let root_dir_url = self.fs_resolver.root_dir_url();
|
|
||||||
debug_assert!(root_dir_url.as_str().ends_with('/'));
|
|
||||||
specifier.as_ref().starts_with(root_dir_url.as_str())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Adds package requirements to the resolver and ensures everything is setup.
|
/// Adds package requirements to the resolver and ensures everything is setup.
|
||||||
pub async fn add_package_reqs(
|
pub async fn add_package_reqs(
|
||||||
&self,
|
&self,
|
||||||
|
@ -273,10 +264,9 @@ impl NpmResolver for CliNpmResolver {
|
||||||
|
|
||||||
fn resolve_package_folder_from_path(
|
fn resolve_package_folder_from_path(
|
||||||
&self,
|
&self,
|
||||||
path: &Path,
|
specifier: &ModuleSpecifier,
|
||||||
) -> Result<Option<PathBuf>, AnyError> {
|
) -> Result<Option<PathBuf>, AnyError> {
|
||||||
let specifier = path_to_specifier(path)?;
|
self.resolve_package_folder_from_specifier(specifier)
|
||||||
self.resolve_package_folder_from_specifier(&specifier)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_package_folder_from_deno_module(
|
fn resolve_package_folder_from_deno_module(
|
||||||
|
@ -295,10 +285,9 @@ impl NpmResolver for CliNpmResolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn in_npm_package(&self, specifier: &ModuleSpecifier) -> bool {
|
fn in_npm_package(&self, specifier: &ModuleSpecifier) -> bool {
|
||||||
self
|
let root_dir_url = self.fs_resolver.root_dir_url();
|
||||||
.resolve_package_folder_from_specifier(specifier)
|
debug_assert!(root_dir_url.as_str().ends_with('/'));
|
||||||
.map(|p| p.is_some())
|
specifier.as_ref().starts_with(root_dir_url.as_str())
|
||||||
.unwrap_or(false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ensure_read_permission(
|
fn ensure_read_permission(
|
||||||
|
@ -338,10 +327,3 @@ pub fn create_npm_fs_resolver(
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn path_to_specifier(path: &Path) -> Result<ModuleSpecifier, AnyError> {
|
|
||||||
match ModuleSpecifier::from_file_path(path.to_path_buf().clean()) {
|
|
||||||
Ok(specifier) => Ok(specifier),
|
|
||||||
Err(()) => bail!("Could not convert '{}' to url.", path.display()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -87,7 +87,7 @@ pub trait NpmResolver: std::fmt::Debug + MaybeSend + MaybeSync {
|
||||||
/// Resolves the npm package folder path from the specified path.
|
/// Resolves the npm package folder path from the specified path.
|
||||||
fn resolve_package_folder_from_path(
|
fn resolve_package_folder_from_path(
|
||||||
&self,
|
&self,
|
||||||
path: &Path,
|
specifier: &ModuleSpecifier,
|
||||||
) -> Result<Option<PathBuf>, AnyError>;
|
) -> Result<Option<PathBuf>, AnyError>;
|
||||||
|
|
||||||
/// Resolves an npm package folder path from a Deno module.
|
/// Resolves an npm package folder path from a Deno module.
|
||||||
|
|
|
@ -1133,7 +1133,7 @@ impl NodeResolver {
|
||||||
) -> Result<Option<PackageJson>, AnyError> {
|
) -> Result<Option<PackageJson>, AnyError> {
|
||||||
let Some(root_folder) = self
|
let Some(root_folder) = self
|
||||||
.npm_resolver
|
.npm_resolver
|
||||||
.resolve_package_folder_from_path(&referrer.to_file_path().unwrap())?
|
.resolve_package_folder_from_path(referrer)?
|
||||||
else {
|
else {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
|
@ -1170,9 +1170,10 @@ impl NodeResolver {
|
||||||
if self.fs.exists_sync(&package_json_path) {
|
if self.fs.exists_sync(&package_json_path) {
|
||||||
return Ok(Some(package_json_path));
|
return Ok(Some(package_json_path));
|
||||||
}
|
}
|
||||||
let Some(root_pkg_folder) = self
|
let Some(root_pkg_folder) =
|
||||||
.npm_resolver
|
self.npm_resolver.resolve_package_folder_from_path(
|
||||||
.resolve_package_folder_from_path(current_dir)?
|
&ModuleSpecifier::from_directory_path(current_dir).unwrap(),
|
||||||
|
)?
|
||||||
else {
|
else {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue