2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-10-28 11:03:33 -04:00
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
use std::collections::HashMap;
|
2022-10-28 11:03:33 -04:00
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::fmt;
|
|
|
|
use std::fmt::Write;
|
|
|
|
|
|
|
|
use deno_ast::ModuleSpecifier;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::resolve_url_or_path;
|
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_graph::Dependency;
|
2023-06-06 17:07:46 -04:00
|
|
|
use deno_graph::GraphKind;
|
2022-10-28 11:03:33 -04:00
|
|
|
use deno_graph::Module;
|
2023-03-21 11:46:40 -04:00
|
|
|
use deno_graph::ModuleError;
|
2022-10-28 11:03:33 -04:00
|
|
|
use deno_graph::ModuleGraph;
|
2023-02-09 22:00:23 -05:00
|
|
|
use deno_graph::Resolution;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_npm::resolution::NpmResolutionSnapshot;
|
|
|
|
use deno_npm::NpmPackageId;
|
|
|
|
use deno_npm::NpmResolutionPackage;
|
2022-10-28 11:03:33 -04:00
|
|
|
use deno_runtime::colors;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_semver::npm::NpmPackageNvReference;
|
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
2023-08-21 05:53:52 -04:00
|
|
|
use deno_semver::package::PackageNv;
|
2022-10-28 11:03:33 -04:00
|
|
|
|
|
|
|
use crate::args::Flags;
|
|
|
|
use crate::args::InfoFlags;
|
|
|
|
use crate::display;
|
2023-05-01 14:35:23 -04:00
|
|
|
use crate::factory::CliFactory;
|
2023-03-13 17:04:00 -04:00
|
|
|
use crate::graph_util::graph_lock_or_exit;
|
2023-04-21 21:02:46 -04:00
|
|
|
use crate::npm::CliNpmResolver;
|
2023-09-29 09:26:25 -04:00
|
|
|
use crate::npm::ManagedCliNpmResolver;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::checksum;
|
2022-10-28 11:03:33 -04:00
|
|
|
|
|
|
|
pub async fn info(flags: Flags, info_flags: InfoFlags) -> Result<(), AnyError> {
|
2023-05-01 14:35:23 -04:00
|
|
|
let factory = CliFactory::from_flags(flags).await?;
|
|
|
|
let cli_options = factory.cli_options();
|
2022-10-28 11:03:33 -04:00
|
|
|
if let Some(specifier) = info_flags.file {
|
2023-05-01 14:35:23 -04:00
|
|
|
let module_graph_builder = factory.module_graph_builder().await?;
|
|
|
|
let npm_resolver = factory.npm_resolver().await?;
|
|
|
|
let maybe_lockfile = factory.maybe_lockfile();
|
2023-07-25 12:23:43 -04:00
|
|
|
let maybe_imports_map = factory.maybe_import_map().await?;
|
|
|
|
|
|
|
|
let maybe_import_specifier = if let Some(imports_map) = maybe_imports_map {
|
|
|
|
if let Ok(imports_specifier) =
|
|
|
|
imports_map.resolve(&specifier, imports_map.base_url())
|
|
|
|
{
|
|
|
|
Some(imports_specifier)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let specifier = match maybe_import_specifier {
|
|
|
|
Some(specifier) => specifier,
|
|
|
|
None => resolve_url_or_path(&specifier, cli_options.initial_cwd())?,
|
|
|
|
};
|
|
|
|
|
2023-05-01 14:35:23 -04:00
|
|
|
let mut loader = module_graph_builder.create_graph_loader();
|
2023-02-08 19:45:04 -05:00
|
|
|
loader.enable_loading_cache_info(); // for displaying the cache information
|
2023-05-01 14:35:23 -04:00
|
|
|
let graph = module_graph_builder
|
2023-06-06 17:07:46 -04:00
|
|
|
.create_graph_with_loader(GraphKind::All, vec![specifier], &mut loader)
|
2023-02-08 19:45:04 -05:00
|
|
|
.await?;
|
2022-10-28 11:03:33 -04:00
|
|
|
|
2023-05-01 14:35:23 -04:00
|
|
|
if let Some(lockfile) = maybe_lockfile {
|
2023-03-13 17:04:00 -04:00
|
|
|
graph_lock_or_exit(&graph, &mut lockfile.lock());
|
|
|
|
}
|
|
|
|
|
2022-10-28 11:03:33 -04:00
|
|
|
if info_flags.json {
|
2022-10-31 10:25:46 -04:00
|
|
|
let mut json_graph = json!(graph);
|
2023-09-29 09:26:25 -04:00
|
|
|
add_npm_packages_to_json(&mut json_graph, npm_resolver.as_ref());
|
2022-10-31 10:25:46 -04:00
|
|
|
display::write_json_to_stdout(&json_graph)?;
|
2022-10-28 11:03:33 -04:00
|
|
|
} else {
|
|
|
|
let mut output = String::new();
|
2023-09-29 09:26:25 -04:00
|
|
|
GraphDisplayContext::write(&graph, npm_resolver.as_ref(), &mut output)?;
|
2022-10-28 11:03:33 -04:00
|
|
|
display::write_to_stdout_ignore_sigpipe(output.as_bytes())?;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If it was just "deno info" print location of caches and exit
|
2022-12-08 11:50:09 -05:00
|
|
|
print_cache_info(
|
2023-05-01 14:35:23 -04:00
|
|
|
&factory,
|
2022-12-08 11:50:09 -05:00
|
|
|
info_flags.json,
|
2023-05-01 14:35:23 -04:00
|
|
|
cli_options.location_flag().as_ref(),
|
2022-12-08 11:50:09 -05:00
|
|
|
)?;
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_cache_info(
|
2023-05-01 14:35:23 -04:00
|
|
|
factory: &CliFactory,
|
2022-10-28 11:03:33 -04:00
|
|
|
json: bool,
|
|
|
|
location: Option<&deno_core::url::Url>,
|
|
|
|
) -> Result<(), AnyError> {
|
2023-05-01 14:35:23 -04:00
|
|
|
let dir = factory.deno_dir()?;
|
2023-08-01 20:49:09 -04:00
|
|
|
#[allow(deprecated)]
|
|
|
|
let modules_cache = factory.global_http_cache()?.get_global_cache_location();
|
2023-10-02 17:53:55 -04:00
|
|
|
let npm_cache = factory.deno_dir()?.npm_folder_path();
|
2023-05-01 14:35:23 -04:00
|
|
|
let typescript_cache = &dir.gen_cache.location;
|
|
|
|
let registry_cache = dir.registries_folder_path();
|
|
|
|
let mut origin_dir = dir.origin_data_folder_path();
|
|
|
|
let deno_dir = dir.root_path_for_display().to_string();
|
2022-10-28 11:03:33 -04:00
|
|
|
|
|
|
|
if let Some(location) = &location {
|
|
|
|
origin_dir =
|
2022-12-17 17:20:15 -05:00
|
|
|
origin_dir.join(checksum::gen(&[location.to_string().as_bytes()]));
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let local_storage_dir = origin_dir.join("local_storage");
|
|
|
|
|
|
|
|
if json {
|
|
|
|
let mut output = json!({
|
2023-05-01 14:35:23 -04:00
|
|
|
"denoDir": deno_dir,
|
2022-10-28 11:03:33 -04:00
|
|
|
"modulesCache": modules_cache,
|
|
|
|
"npmCache": npm_cache,
|
|
|
|
"typescriptCache": typescript_cache,
|
|
|
|
"registryCache": registry_cache,
|
|
|
|
"originStorage": origin_dir,
|
|
|
|
});
|
|
|
|
|
|
|
|
if location.is_some() {
|
|
|
|
output["localStorage"] = serde_json::to_value(local_storage_dir)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
display::write_json_to_stdout(&output)
|
|
|
|
} else {
|
2022-11-25 19:04:30 -05:00
|
|
|
println!("{} {}", colors::bold("DENO_DIR location:"), deno_dir);
|
2022-10-28 11:03:33 -04:00
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
colors::bold("Remote modules cache:"),
|
|
|
|
modules_cache.display()
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
colors::bold("npm modules cache:"),
|
|
|
|
npm_cache.display()
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
colors::bold("Emitted modules cache:"),
|
|
|
|
typescript_cache.display()
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
colors::bold("Language server registries cache:"),
|
|
|
|
registry_cache.display(),
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
colors::bold("Origin storage:"),
|
|
|
|
origin_dir.display()
|
|
|
|
);
|
|
|
|
if location.is_some() {
|
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
colors::bold("Local Storage:"),
|
|
|
|
local_storage_dir.display(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-31 10:25:46 -04:00
|
|
|
fn add_npm_packages_to_json(
|
|
|
|
json: &mut serde_json::Value,
|
2023-09-29 09:26:25 -04:00
|
|
|
npm_resolver: &dyn CliNpmResolver,
|
2022-10-31 10:25:46 -04:00
|
|
|
) {
|
2023-09-29 09:26:25 -04:00
|
|
|
let Some(npm_resolver) = npm_resolver.as_managed() else {
|
|
|
|
return; // does not include byonm to deno info's output
|
|
|
|
};
|
|
|
|
|
2022-10-31 10:25:46 -04:00
|
|
|
// ideally deno_graph could handle this, but for now we just modify the json here
|
|
|
|
let snapshot = npm_resolver.snapshot();
|
|
|
|
let json = json.as_object_mut().unwrap();
|
|
|
|
let modules = json.get_mut("modules").and_then(|m| m.as_array_mut());
|
|
|
|
if let Some(modules) = modules {
|
|
|
|
if modules.len() == 1
|
2023-02-22 14:15:25 -05:00
|
|
|
&& modules[0].get("kind").and_then(|k| k.as_str()) == Some("npm")
|
2022-10-31 10:25:46 -04:00
|
|
|
{
|
|
|
|
// If there is only one module and it's "external", then that means
|
|
|
|
// someone provided an npm specifier as a cli argument. In this case,
|
|
|
|
// we want to show which npm package the cli argument resolved to.
|
|
|
|
let module = &mut modules[0];
|
|
|
|
let maybe_package = module
|
|
|
|
.get("specifier")
|
|
|
|
.and_then(|k| k.as_str())
|
2023-02-22 14:15:25 -05:00
|
|
|
.and_then(|specifier| NpmPackageNvReference::from_str(specifier).ok())
|
2022-10-31 10:25:46 -04:00
|
|
|
.and_then(|package_ref| {
|
|
|
|
snapshot
|
2023-08-21 05:53:52 -04:00
|
|
|
.resolve_package_from_deno_module(package_ref.nv())
|
2022-10-31 10:25:46 -04:00
|
|
|
.ok()
|
|
|
|
});
|
|
|
|
if let Some(pkg) = maybe_package {
|
|
|
|
if let Some(module) = module.as_object_mut() {
|
2023-05-24 16:23:10 -04:00
|
|
|
module
|
|
|
|
.insert("npmPackage".to_string(), pkg.id.as_serialized().into());
|
2022-10-31 10:25:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Filter out npm package references from the modules and instead
|
|
|
|
// have them only listed as dependencies. This is done because various
|
|
|
|
// npm specifiers modules in the graph are really just unresolved
|
|
|
|
// references. So there could be listed multiple npm specifiers
|
|
|
|
// that would resolve to a single npm package.
|
|
|
|
for i in (0..modules.len()).rev() {
|
2023-02-22 14:15:25 -05:00
|
|
|
if matches!(
|
|
|
|
modules[i].get("kind").and_then(|k| k.as_str()),
|
|
|
|
Some("npm") | Some("external")
|
|
|
|
) {
|
2022-10-31 10:25:46 -04:00
|
|
|
modules.remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for module in modules.iter_mut() {
|
|
|
|
let dependencies = module
|
|
|
|
.get_mut("dependencies")
|
|
|
|
.and_then(|d| d.as_array_mut());
|
|
|
|
if let Some(dependencies) = dependencies {
|
|
|
|
for dep in dependencies.iter_mut() {
|
|
|
|
if let serde_json::Value::Object(dep) = dep {
|
|
|
|
let specifier = dep.get("specifier").and_then(|s| s.as_str());
|
|
|
|
if let Some(specifier) = specifier {
|
2023-02-21 12:03:48 -05:00
|
|
|
if let Ok(npm_ref) = NpmPackageReqReference::from_str(specifier) {
|
2023-08-21 05:53:52 -04:00
|
|
|
if let Ok(pkg) =
|
|
|
|
snapshot.resolve_pkg_from_pkg_req(npm_ref.req())
|
2022-10-31 10:25:46 -04:00
|
|
|
{
|
|
|
|
dep.insert(
|
|
|
|
"npmPackage".to_string(),
|
2023-05-24 16:23:10 -04:00
|
|
|
pkg.id.as_serialized().into(),
|
2022-10-31 10:25:46 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-17 17:38:50 -04:00
|
|
|
let mut sorted_packages =
|
|
|
|
snapshot.all_packages_for_every_system().collect::<Vec<_>>();
|
2023-05-24 16:23:10 -04:00
|
|
|
sorted_packages.sort_by(|a, b| a.id.cmp(&b.id));
|
2022-10-31 10:25:46 -04:00
|
|
|
let mut json_packages = serde_json::Map::with_capacity(sorted_packages.len());
|
|
|
|
for pkg in sorted_packages {
|
|
|
|
let mut kv = serde_json::Map::new();
|
2023-05-24 16:23:10 -04:00
|
|
|
kv.insert("name".to_string(), pkg.id.nv.name.to_string().into());
|
|
|
|
kv.insert("version".to_string(), pkg.id.nv.version.to_string().into());
|
2022-10-31 10:25:46 -04:00
|
|
|
let mut deps = pkg.dependencies.values().collect::<Vec<_>>();
|
|
|
|
deps.sort();
|
|
|
|
let deps = deps
|
|
|
|
.into_iter()
|
2022-11-08 14:17:24 -05:00
|
|
|
.map(|id| serde_json::Value::String(id.as_serialized()))
|
2022-10-31 10:25:46 -04:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
kv.insert("dependencies".to_string(), deps.into());
|
|
|
|
|
2023-05-24 16:23:10 -04:00
|
|
|
json_packages.insert(pkg.id.as_serialized(), kv.into());
|
2022-10-31 10:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
json.insert("npmPackages".to_string(), json_packages.into());
|
|
|
|
}
|
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
struct TreeNode {
|
|
|
|
text: String,
|
|
|
|
children: Vec<TreeNode>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TreeNode {
|
|
|
|
pub fn from_text(text: String) -> Self {
|
|
|
|
Self {
|
|
|
|
text,
|
|
|
|
children: Default::default(),
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
fn print_tree_node<TWrite: Write>(
|
|
|
|
tree_node: &TreeNode,
|
|
|
|
writer: &mut TWrite,
|
2022-10-28 11:03:33 -04:00
|
|
|
) -> fmt::Result {
|
2022-10-28 16:19:55 -04:00
|
|
|
fn print_children<TWrite: Write>(
|
|
|
|
writer: &mut TWrite,
|
|
|
|
prefix: &str,
|
|
|
|
children: &Vec<TreeNode>,
|
|
|
|
) -> fmt::Result {
|
|
|
|
const SIBLING_CONNECTOR: char = '├';
|
|
|
|
const LAST_SIBLING_CONNECTOR: char = '└';
|
|
|
|
const CHILD_DEPS_CONNECTOR: char = '┬';
|
|
|
|
const CHILD_NO_DEPS_CONNECTOR: char = '─';
|
|
|
|
const VERTICAL_CONNECTOR: char = '│';
|
|
|
|
const EMPTY_CONNECTOR: char = ' ';
|
|
|
|
|
|
|
|
let child_len = children.len();
|
|
|
|
for (index, child) in children.iter().enumerate() {
|
|
|
|
let is_last = index + 1 == child_len;
|
|
|
|
let sibling_connector = if is_last {
|
|
|
|
LAST_SIBLING_CONNECTOR
|
|
|
|
} else {
|
|
|
|
SIBLING_CONNECTOR
|
|
|
|
};
|
|
|
|
let child_connector = if child.children.is_empty() {
|
|
|
|
CHILD_NO_DEPS_CONNECTOR
|
|
|
|
} else {
|
|
|
|
CHILD_DEPS_CONNECTOR
|
|
|
|
};
|
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
"{} {}",
|
2023-01-27 10:43:16 -05:00
|
|
|
colors::gray(format!("{prefix}{sibling_connector}─{child_connector}")),
|
2022-10-28 16:19:55 -04:00
|
|
|
child.text
|
|
|
|
)?;
|
|
|
|
let child_prefix = format!(
|
|
|
|
"{}{}{}",
|
|
|
|
prefix,
|
|
|
|
if is_last {
|
|
|
|
EMPTY_CONNECTOR
|
|
|
|
} else {
|
|
|
|
VERTICAL_CONNECTOR
|
|
|
|
},
|
|
|
|
EMPTY_CONNECTOR
|
|
|
|
);
|
|
|
|
print_children(writer, &child_prefix, &child.children)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
2022-10-28 16:19:55 -04:00
|
|
|
|
|
|
|
writeln!(writer, "{}", tree_node.text)?;
|
|
|
|
print_children(writer, "", &tree_node.children)?;
|
2022-10-28 11:03:33 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
/// Precached information about npm packages that are used in deno info.
|
|
|
|
#[derive(Default)]
|
|
|
|
struct NpmInfo {
|
2023-02-21 12:03:48 -05:00
|
|
|
package_sizes: HashMap<NpmPackageId, u64>,
|
2023-08-21 05:53:52 -04:00
|
|
|
resolved_ids: HashMap<PackageNv, NpmPackageId>,
|
2023-02-21 12:03:48 -05:00
|
|
|
packages: HashMap<NpmPackageId, NpmResolutionPackage>,
|
2022-10-28 16:19:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NpmInfo {
|
|
|
|
pub fn build<'a>(
|
|
|
|
graph: &'a ModuleGraph,
|
2023-09-29 09:26:25 -04:00
|
|
|
npm_resolver: &'a ManagedCliNpmResolver,
|
2022-10-28 16:19:55 -04:00
|
|
|
npm_snapshot: &'a NpmResolutionSnapshot,
|
|
|
|
) -> Self {
|
|
|
|
let mut info = NpmInfo::default();
|
2023-02-22 14:15:25 -05:00
|
|
|
if graph.npm_packages.is_empty() {
|
|
|
|
return info; // skip going over the modules if there's no npm packages
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
2022-10-28 16:19:55 -04:00
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
for module in graph.modules() {
|
|
|
|
if let Module::Npm(module) = module {
|
2023-08-21 05:53:52 -04:00
|
|
|
let nv = module.nv_reference.nv();
|
2023-02-22 14:15:25 -05:00
|
|
|
if let Ok(package) = npm_snapshot.resolve_package_from_deno_module(nv) {
|
2023-05-24 16:23:10 -04:00
|
|
|
info.resolved_ids.insert(nv.clone(), package.id.clone());
|
|
|
|
if !info.packages.contains_key(&package.id) {
|
2022-10-28 16:19:55 -04:00
|
|
|
info.fill_package_info(package, npm_resolver, npm_snapshot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
2022-10-28 16:19:55 -04:00
|
|
|
|
|
|
|
info
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
fn fill_package_info<'a>(
|
|
|
|
&mut self,
|
|
|
|
package: &NpmResolutionPackage,
|
2023-09-29 09:26:25 -04:00
|
|
|
npm_resolver: &'a ManagedCliNpmResolver,
|
2022-10-28 16:19:55 -04:00
|
|
|
npm_snapshot: &'a NpmResolutionSnapshot,
|
|
|
|
) {
|
2023-05-24 16:23:10 -04:00
|
|
|
self.packages.insert(package.id.clone(), package.clone());
|
|
|
|
if let Ok(size) = npm_resolver.package_size(&package.id) {
|
|
|
|
self.package_sizes.insert(package.id.clone(), size);
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
2022-10-28 16:19:55 -04:00
|
|
|
for id in package.dependencies.values() {
|
|
|
|
if !self.packages.contains_key(id) {
|
|
|
|
if let Some(package) = npm_snapshot.package_from_id(id) {
|
|
|
|
self.fill_package_info(package, npm_resolver, npm_snapshot);
|
|
|
|
}
|
|
|
|
}
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
2022-10-28 16:19:55 -04:00
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
pub fn resolve_package(
|
2022-10-28 16:19:55 -04:00
|
|
|
&self,
|
2023-08-21 05:53:52 -04:00
|
|
|
nv: &PackageNv,
|
2022-10-28 16:19:55 -04:00
|
|
|
) -> Option<&NpmResolutionPackage> {
|
2023-02-22 14:15:25 -05:00
|
|
|
let id = self.resolved_ids.get(nv)?;
|
|
|
|
self.packages.get(id)
|
2022-10-28 16:19:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GraphDisplayContext<'a> {
|
|
|
|
graph: &'a ModuleGraph,
|
|
|
|
npm_info: NpmInfo,
|
|
|
|
seen: HashSet<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> GraphDisplayContext<'a> {
|
|
|
|
pub fn write<TWrite: Write>(
|
|
|
|
graph: &'a ModuleGraph,
|
2023-09-29 09:26:25 -04:00
|
|
|
npm_resolver: &'a dyn CliNpmResolver,
|
2022-10-28 16:19:55 -04:00
|
|
|
writer: &mut TWrite,
|
|
|
|
) -> fmt::Result {
|
2023-09-29 09:26:25 -04:00
|
|
|
let npm_info = match npm_resolver.as_managed() {
|
|
|
|
Some(npm_resolver) => {
|
|
|
|
let npm_snapshot = npm_resolver.snapshot();
|
|
|
|
NpmInfo::build(graph, npm_resolver, &npm_snapshot)
|
|
|
|
}
|
|
|
|
None => NpmInfo::default(),
|
|
|
|
};
|
2022-10-28 16:19:55 -04:00
|
|
|
Self {
|
|
|
|
graph,
|
|
|
|
npm_info,
|
|
|
|
seen: Default::default(),
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
2022-10-28 16:19:55 -04:00
|
|
|
.into_writer(writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_writer<TWrite: Write>(mut self, writer: &mut TWrite) -> fmt::Result {
|
|
|
|
if self.graph.roots.is_empty() || self.graph.roots.len() > 1 {
|
|
|
|
return writeln!(
|
|
|
|
writer,
|
|
|
|
"{} displaying graphs that have multiple roots is not supported.",
|
|
|
|
colors::red("error:")
|
|
|
|
);
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
2022-10-28 16:19:55 -04:00
|
|
|
|
2023-01-24 08:23:19 -05:00
|
|
|
let root_specifier = self.graph.resolve(&self.graph.roots[0]);
|
2022-10-28 16:19:55 -04:00
|
|
|
match self.graph.try_get(&root_specifier) {
|
|
|
|
Ok(Some(root)) => {
|
2023-02-22 14:15:25 -05:00
|
|
|
let maybe_cache_info = match root {
|
|
|
|
Module::Esm(module) => module.maybe_cache_info.as_ref(),
|
|
|
|
Module::Json(module) => module.maybe_cache_info.as_ref(),
|
|
|
|
Module::Node(_) | Module::Npm(_) | Module::External(_) => None,
|
|
|
|
};
|
|
|
|
if let Some(cache_info) = maybe_cache_info {
|
2022-10-28 16:19:55 -04:00
|
|
|
if let Some(local) = &cache_info.local {
|
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
"{} {}",
|
|
|
|
colors::bold("local:"),
|
|
|
|
local.to_string_lossy()
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
if let Some(emit) = &cache_info.emit {
|
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
"{} {}",
|
|
|
|
colors::bold("emit:"),
|
|
|
|
emit.to_string_lossy()
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
if let Some(map) = &cache_info.map {
|
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
"{} {}",
|
|
|
|
colors::bold("map:"),
|
|
|
|
map.to_string_lossy()
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
2023-02-22 14:15:25 -05:00
|
|
|
if let Some(module) = root.esm() {
|
|
|
|
writeln!(writer, "{} {}", colors::bold("type:"), module.media_type)?;
|
|
|
|
}
|
|
|
|
let total_modules_size = self
|
|
|
|
.graph
|
|
|
|
.modules()
|
|
|
|
.map(|m| {
|
|
|
|
let size = match m {
|
|
|
|
Module::Esm(module) => module.size(),
|
|
|
|
Module::Json(module) => module.size(),
|
|
|
|
Module::Node(_) | Module::Npm(_) | Module::External(_) => 0,
|
|
|
|
};
|
|
|
|
size as f64
|
|
|
|
})
|
|
|
|
.sum::<f64>();
|
2022-10-28 16:19:55 -04:00
|
|
|
let total_npm_package_size = self
|
|
|
|
.npm_info
|
|
|
|
.package_sizes
|
|
|
|
.values()
|
|
|
|
.map(|s| *s as f64)
|
|
|
|
.sum::<f64>();
|
|
|
|
let total_size = total_modules_size + total_npm_package_size;
|
2023-02-22 14:15:25 -05:00
|
|
|
let dep_count = self.graph.modules().count() - 1 // -1 for the root module
|
2022-12-06 14:12:51 -05:00
|
|
|
+ self.npm_info.packages.len()
|
2023-02-22 14:15:25 -05:00
|
|
|
- self.npm_info.resolved_ids.len();
|
2022-10-28 16:19:55 -04:00
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
"{} {} unique",
|
|
|
|
colors::bold("dependencies:"),
|
|
|
|
dep_count,
|
|
|
|
)?;
|
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
"{} {}",
|
|
|
|
colors::bold("size:"),
|
|
|
|
display::human_size(total_size),
|
|
|
|
)?;
|
|
|
|
writeln!(writer)?;
|
|
|
|
let root_node = self.build_module_info(root, false);
|
|
|
|
print_tree_node(&root_node, writer)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(err) => {
|
2023-10-24 17:43:19 -04:00
|
|
|
if let ModuleError::Missing(_, _) = *err {
|
2023-02-09 22:00:23 -05:00
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
"{} module could not be found",
|
|
|
|
colors::red("error:")
|
|
|
|
)
|
|
|
|
} else {
|
2023-04-12 08:36:11 -04:00
|
|
|
writeln!(writer, "{} {:#}", colors::red("error:"), err)
|
2023-02-09 22:00:23 -05:00
|
|
|
}
|
2022-10-28 16:19:55 -04:00
|
|
|
}
|
|
|
|
Ok(None) => {
|
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
"{} an internal error occurred",
|
|
|
|
colors::red("error:")
|
|
|
|
)
|
|
|
|
}
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
2022-10-28 16:19:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn build_dep_info(&mut self, dep: &Dependency) -> Vec<TreeNode> {
|
|
|
|
let mut children = Vec::with_capacity(2);
|
|
|
|
if !dep.maybe_code.is_none() {
|
|
|
|
if let Some(child) = self.build_resolved_info(&dep.maybe_code, false) {
|
|
|
|
children.push(child);
|
|
|
|
}
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
2022-10-28 16:19:55 -04:00
|
|
|
if !dep.maybe_type.is_none() {
|
|
|
|
if let Some(child) = self.build_resolved_info(&dep.maybe_type, true) {
|
|
|
|
children.push(child);
|
|
|
|
}
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
2022-10-28 16:19:55 -04:00
|
|
|
children
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
fn build_module_info(&mut self, module: &Module, type_dep: bool) -> TreeNode {
|
|
|
|
enum PackageOrSpecifier {
|
|
|
|
Package(NpmResolutionPackage),
|
|
|
|
Specifier(ModuleSpecifier),
|
|
|
|
}
|
2022-10-28 11:03:33 -04:00
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
use PackageOrSpecifier::*;
|
2022-10-28 11:03:33 -04:00
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
let package_or_specifier = match module.npm() {
|
2023-08-21 05:53:52 -04:00
|
|
|
Some(npm) => match self.npm_info.resolve_package(npm.nv_reference.nv()) {
|
2022-10-28 16:19:55 -04:00
|
|
|
Some(package) => Package(package.clone()),
|
2023-02-22 14:15:25 -05:00
|
|
|
None => Specifier(module.specifier().clone()), // should never happen
|
|
|
|
},
|
|
|
|
None => Specifier(module.specifier().clone()),
|
|
|
|
};
|
2022-10-28 16:19:55 -04:00
|
|
|
let was_seen = !self.seen.insert(match &package_or_specifier {
|
2023-05-24 16:23:10 -04:00
|
|
|
Package(package) => package.id.as_serialized(),
|
2022-10-28 16:19:55 -04:00
|
|
|
Specifier(specifier) => specifier.to_string(),
|
|
|
|
});
|
|
|
|
let header_text = if was_seen {
|
|
|
|
let specifier_str = if type_dep {
|
2023-02-22 14:15:25 -05:00
|
|
|
colors::italic_gray(module.specifier()).to_string()
|
2022-10-28 16:19:55 -04:00
|
|
|
} else {
|
2023-02-22 14:15:25 -05:00
|
|
|
colors::gray(module.specifier()).to_string()
|
2022-10-28 16:19:55 -04:00
|
|
|
};
|
|
|
|
format!("{} {}", specifier_str, colors::gray("*"))
|
|
|
|
} else {
|
2023-02-22 14:15:25 -05:00
|
|
|
let header_text = if type_dep {
|
|
|
|
colors::italic(module.specifier()).to_string()
|
2022-10-28 16:19:55 -04:00
|
|
|
} else {
|
2023-02-22 14:15:25 -05:00
|
|
|
module.specifier().to_string()
|
2022-10-28 16:19:55 -04:00
|
|
|
};
|
|
|
|
let maybe_size = match &package_or_specifier {
|
2022-12-17 17:20:15 -05:00
|
|
|
Package(package) => {
|
2023-05-24 16:23:10 -04:00
|
|
|
self.npm_info.package_sizes.get(&package.id).copied()
|
2022-12-17 17:20:15 -05:00
|
|
|
}
|
2023-02-22 14:15:25 -05:00
|
|
|
Specifier(_) => match module {
|
|
|
|
Module::Esm(module) => Some(module.size() as u64),
|
|
|
|
Module::Json(module) => Some(module.size() as u64),
|
|
|
|
Module::Node(_) | Module::Npm(_) | Module::External(_) => None,
|
|
|
|
},
|
2022-10-28 16:19:55 -04:00
|
|
|
};
|
|
|
|
format!("{} {}", header_text, maybe_size_to_text(maybe_size))
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut tree_node = TreeNode::from_text(header_text);
|
|
|
|
|
|
|
|
if !was_seen {
|
|
|
|
match &package_or_specifier {
|
|
|
|
Package(package) => {
|
|
|
|
tree_node.children.extend(self.build_npm_deps(package));
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
2022-10-28 16:19:55 -04:00
|
|
|
Specifier(_) => {
|
2023-02-22 14:15:25 -05:00
|
|
|
if let Some(module) = module.esm() {
|
|
|
|
if let Some(types_dep) = &module.maybe_types_dependency {
|
|
|
|
if let Some(child) =
|
|
|
|
self.build_resolved_info(&types_dep.dependency, true)
|
|
|
|
{
|
|
|
|
tree_node.children.push(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for dep in module.dependencies.values() {
|
|
|
|
tree_node.children.extend(self.build_dep_info(dep));
|
|
|
|
}
|
2022-10-28 16:19:55 -04:00
|
|
|
}
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
2022-10-28 16:19:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tree_node
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_npm_deps(
|
|
|
|
&mut self,
|
|
|
|
package: &NpmResolutionPackage,
|
|
|
|
) -> Vec<TreeNode> {
|
|
|
|
let mut deps = package.dependencies.values().collect::<Vec<_>>();
|
|
|
|
deps.sort();
|
|
|
|
let mut children = Vec::with_capacity(deps.len());
|
|
|
|
for dep_id in deps.into_iter() {
|
|
|
|
let maybe_size = self.npm_info.package_sizes.get(dep_id).cloned();
|
|
|
|
let size_str = maybe_size_to_text(maybe_size);
|
2022-11-08 14:17:24 -05:00
|
|
|
let mut child = TreeNode::from_text(format!(
|
2023-08-21 05:53:52 -04:00
|
|
|
"npm:/{} {}",
|
2022-11-08 14:17:24 -05:00
|
|
|
dep_id.as_serialized(),
|
|
|
|
size_str
|
|
|
|
));
|
2022-10-28 16:19:55 -04:00
|
|
|
if let Some(package) = self.npm_info.packages.get(dep_id) {
|
|
|
|
if !package.dependencies.is_empty() {
|
2023-05-24 16:23:10 -04:00
|
|
|
let was_seen = !self.seen.insert(package.id.as_serialized());
|
2022-11-17 21:22:28 -05:00
|
|
|
if was_seen {
|
2022-10-28 16:19:55 -04:00
|
|
|
child.text = format!("{} {}", child.text, colors::gray("*"));
|
|
|
|
} else {
|
|
|
|
let package = package.clone();
|
|
|
|
child.children.extend(self.build_npm_deps(&package));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
children.push(child);
|
|
|
|
}
|
|
|
|
children
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_error_info(
|
|
|
|
&mut self,
|
2023-10-24 17:43:19 -04:00
|
|
|
err: &ModuleError,
|
2022-10-28 16:19:55 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> TreeNode {
|
|
|
|
self.seen.insert(specifier.to_string());
|
|
|
|
match err {
|
2023-10-24 17:43:19 -04:00
|
|
|
ModuleError::InvalidTypeAssertion { .. } => {
|
|
|
|
self.build_error_msg(specifier, "(invalid import attribute)")
|
|
|
|
}
|
|
|
|
ModuleError::LoadingErr(_, _, _) => {
|
|
|
|
self.build_error_msg(specifier, "(loading error)")
|
|
|
|
}
|
|
|
|
ModuleError::ParseErr(_, _) => {
|
|
|
|
self.build_error_msg(specifier, "(parsing error)")
|
|
|
|
}
|
|
|
|
ModuleError::UnsupportedImportAttributeType { .. } => {
|
|
|
|
self.build_error_msg(specifier, "(unsupported import attribute)")
|
|
|
|
}
|
|
|
|
ModuleError::UnsupportedMediaType { .. } => {
|
|
|
|
self.build_error_msg(specifier, "(unsupported)")
|
|
|
|
}
|
|
|
|
ModuleError::Missing(_, _) | ModuleError::MissingDynamic(_, _) => {
|
|
|
|
self.build_error_msg(specifier, "(missing)")
|
|
|
|
}
|
2023-11-10 13:40:39 -05:00
|
|
|
ModuleError::MissingWorkspaceMemberExports { .. } => {
|
|
|
|
self.build_error_msg(specifier, "(missing exports)")
|
|
|
|
}
|
|
|
|
ModuleError::UnknownExport { .. } => {
|
|
|
|
self.build_error_msg(specifier, "(unknown export)")
|
|
|
|
}
|
2023-10-24 17:43:19 -04:00
|
|
|
ModuleError::UnknownPackage { .. } => {
|
|
|
|
self.build_error_msg(specifier, "(unknown package)")
|
|
|
|
}
|
|
|
|
ModuleError::UnknownPackageReq { .. } => {
|
|
|
|
self.build_error_msg(specifier, "(unknown package constraint)")
|
2022-10-28 16:19:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_error_msg(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
error_msg: &str,
|
|
|
|
) -> TreeNode {
|
|
|
|
TreeNode::from_text(format!(
|
|
|
|
"{} {}",
|
|
|
|
colors::red(specifier),
|
|
|
|
colors::red_bold(error_msg)
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_resolved_info(
|
|
|
|
&mut self,
|
2023-02-09 22:00:23 -05:00
|
|
|
resolution: &Resolution,
|
2022-10-28 16:19:55 -04:00
|
|
|
type_dep: bool,
|
|
|
|
) -> Option<TreeNode> {
|
2023-02-09 22:00:23 -05:00
|
|
|
match resolution {
|
|
|
|
Resolution::Ok(resolved) => {
|
|
|
|
let specifier = &resolved.specifier;
|
2022-10-28 16:19:55 -04:00
|
|
|
let resolved_specifier = self.graph.resolve(specifier);
|
|
|
|
Some(match self.graph.try_get(&resolved_specifier) {
|
|
|
|
Ok(Some(module)) => self.build_module_info(module, type_dep),
|
2023-02-09 22:00:23 -05:00
|
|
|
Err(err) => self.build_error_info(err, &resolved_specifier),
|
2022-10-28 16:19:55 -04:00
|
|
|
Ok(None) => TreeNode::from_text(format!(
|
2022-10-28 11:03:33 -04:00
|
|
|
"{} {}",
|
|
|
|
colors::red(specifier),
|
|
|
|
colors::red_bold("(missing)")
|
2022-10-28 16:19:55 -04:00
|
|
|
)),
|
|
|
|
})
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
2023-02-09 22:00:23 -05:00
|
|
|
Resolution::Err(err) => Some(TreeNode::from_text(format!(
|
2022-10-28 11:03:33 -04:00
|
|
|
"{} {}",
|
|
|
|
colors::italic(err.to_string()),
|
|
|
|
colors::red_bold("(resolve error)")
|
2022-10-28 16:19:55 -04:00
|
|
|
))),
|
|
|
|
_ => None,
|
|
|
|
}
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
fn maybe_size_to_text(maybe_size: Option<u64>) -> String {
|
|
|
|
colors::gray(format!(
|
|
|
|
"({})",
|
|
|
|
match maybe_size {
|
|
|
|
Some(size) => display::human_size(size as f64),
|
|
|
|
None => "unknown".to_string(),
|
|
|
|
}
|
|
|
|
))
|
|
|
|
.to_string()
|
2022-10-28 11:03:33 -04:00
|
|
|
}
|