diff --git a/cli/args/deno_json.rs b/cli/args/deno_json.rs new file mode 100644 index 0000000000..60ac5d58cb --- /dev/null +++ b/cli/args/deno_json.rs @@ -0,0 +1,51 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +use std::collections::HashSet; + +use deno_core::serde_json; +use deno_semver::jsr::JsrDepPackageReq; +use deno_semver::jsr::JsrPackageReqReference; +use deno_semver::npm::NpmPackageReqReference; + +pub fn deno_json_deps( + config: &deno_config::ConfigFile, +) -> HashSet { + let values = imports_values(config.json.imports.as_ref()) + .into_iter() + .chain(scope_values(config.json.scopes.as_ref())); + values_to_set(values) +} + +fn imports_values(value: Option<&serde_json::Value>) -> Vec<&String> { + let Some(obj) = value.and_then(|v| v.as_object()) else { + return Vec::new(); + }; + let mut items = Vec::with_capacity(obj.len()); + for value in obj.values() { + if let serde_json::Value::String(value) = value { + items.push(value); + } + } + items +} + +fn scope_values(value: Option<&serde_json::Value>) -> Vec<&String> { + let Some(obj) = value.and_then(|v| v.as_object()) else { + return Vec::new(); + }; + obj.values().flat_map(|v| imports_values(Some(v))).collect() +} + +fn values_to_set<'a>( + values: impl Iterator, +) -> HashSet { + let mut entries = HashSet::new(); + for value in values { + if let Ok(req_ref) = JsrPackageReqReference::from_str(value) { + entries.insert(JsrDepPackageReq::jsr(req_ref.into_inner().req)); + } else if let Ok(req_ref) = NpmPackageReqReference::from_str(value) { + entries.insert(JsrDepPackageReq::npm(req_ref.into_inner().req)); + } + } + entries +} diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 95df047e63..a4904d39dc 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -1,5 +1,6 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +pub mod deno_json; mod flags; mod flags_net; mod import_map; diff --git a/cli/factory.rs b/cli/factory.rs index bee3ea5f6c..54ec6ac5ec 100644 --- a/cli/factory.rs +++ b/cli/factory.rs @@ -1,5 +1,6 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +use crate::args::deno_json::deno_json_deps; use crate::args::CliOptions; use crate::args::DenoSubcommand; use crate::args::Flags; @@ -44,7 +45,6 @@ use crate::resolver::SloppyImportsResolver; use crate::standalone::DenoCompileBinaryWriter; use crate::tools::check::TypeChecker; use crate::tools::coverage::CoverageCollector; -use crate::tools::registry::deno_json_deps; use crate::tools::run::hmr::HmrRunner; use crate::util::file_watcher::WatcherCommunicator; use crate::util::fs::canonicalize_path_maybe_not_exists; diff --git a/cli/tools/registry/mod.rs b/cli/tools/registry/mod.rs index dcfde2297a..4af0f73b98 100644 --- a/cli/tools/registry/mod.rs +++ b/cli/tools/registry/mod.rs @@ -59,7 +59,6 @@ use auth::get_auth_method; use auth::AuthMethod; pub use pm::add; use publish_order::PublishOrderGraph; -pub use unfurl::deno_json_deps; use unfurl::SpecifierUnfurler; use super::check::TypeChecker; diff --git a/cli/tools/registry/unfurl.rs b/cli/tools/registry/unfurl.rs index 3207bbd557..ac270346c4 100644 --- a/cli/tools/registry/unfurl.rs +++ b/cli/tools/registry/unfurl.rs @@ -1,67 +1,18 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -use std::collections::HashSet; - use deno_ast::ParsedSource; use deno_ast::SourceRange; use deno_ast::SourceTextInfo; -use deno_core::serde_json; use deno_core::ModuleSpecifier; use deno_graph::DefaultModuleAnalyzer; use deno_graph::DependencyDescriptor; use deno_graph::DynamicTemplatePart; use deno_graph::TypeScriptReference; use deno_runtime::deno_node::is_builtin_node_module; -use deno_semver::jsr::JsrDepPackageReq; -use deno_semver::jsr::JsrPackageReqReference; -use deno_semver::npm::NpmPackageReqReference; use crate::resolver::MappedSpecifierResolver; use crate::resolver::SloppyImportsResolver; -pub fn deno_json_deps( - config: &deno_config::ConfigFile, -) -> HashSet { - let values = imports_values(config.json.imports.as_ref()) - .into_iter() - .chain(scope_values(config.json.scopes.as_ref())); - values_to_set(values) -} - -fn imports_values(value: Option<&serde_json::Value>) -> Vec<&String> { - let Some(obj) = value.and_then(|v| v.as_object()) else { - return Vec::new(); - }; - let mut items = Vec::with_capacity(obj.len()); - for value in obj.values() { - if let serde_json::Value::String(value) = value { - items.push(value); - } - } - items -} - -fn scope_values(value: Option<&serde_json::Value>) -> Vec<&String> { - let Some(obj) = value.and_then(|v| v.as_object()) else { - return Vec::new(); - }; - obj.values().flat_map(|v| imports_values(Some(v))).collect() -} - -fn values_to_set<'a>( - values: impl Iterator, -) -> HashSet { - let mut entries = HashSet::new(); - for value in values { - if let Ok(req_ref) = JsrPackageReqReference::from_str(value) { - entries.insert(JsrDepPackageReq::jsr(req_ref.into_inner().req)); - } else if let Ok(req_ref) = NpmPackageReqReference::from_str(value) { - entries.insert(JsrDepPackageReq::npm(req_ref.into_inner().req)); - } - } - entries -} - #[derive(Debug, Clone)] pub enum SpecifierUnfurlerDiagnostic { UnanalyzableDynamicImport {