1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

feat(cli): improve deno info output for npm packages (#25906)

This commit is contained in:
Luca Casonato 2024-10-14 13:29:50 +01:00 committed by GitHub
parent bbad7c5922
commit 3eda179220
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 231 additions and 81 deletions

View file

@ -17,6 +17,7 @@ use deno_graph::Module;
use deno_graph::ModuleError;
use deno_graph::ModuleGraph;
use deno_graph::Resolution;
use deno_npm::npm_rc::ResolvedNpmRc;
use deno_npm::resolution::NpmResolutionSnapshot;
use deno_npm::NpmPackageId;
use deno_npm::NpmResolutionPackage;
@ -47,6 +48,7 @@ pub async fn info(
let module_graph_creator = factory.module_graph_creator().await?;
let npm_resolver = factory.npm_resolver().await?;
let maybe_lockfile = cli_options.maybe_lockfile();
let npmrc = cli_options.npmrc();
let resolver = factory.workspace_resolver().await?;
let maybe_import_specifier =
@ -88,7 +90,8 @@ pub async fn info(
JSON_SCHEMA_VERSION.into(),
);
}
add_npm_packages_to_json(&mut json_graph, npm_resolver.as_ref());
add_npm_packages_to_json(&mut json_graph, npm_resolver.as_ref(), npmrc);
display::write_json_to_stdout(&json_graph)?;
} else {
let mut output = String::new();
@ -185,6 +188,7 @@ fn print_cache_info(
fn add_npm_packages_to_json(
json: &mut serde_json::Value,
npm_resolver: &dyn CliNpmResolver,
npmrc: &ResolvedNpmRc,
) {
let Some(npm_resolver) = npm_resolver.as_managed() else {
return; // does not include byonm to deno info's output
@ -195,45 +199,28 @@ fn add_npm_packages_to_json(
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
&& modules[0].get("kind").and_then(|k| k.as_str()) == Some("npm")
{
// 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())
.and_then(|specifier| NpmPackageNvReference::from_str(specifier).ok())
.and_then(|package_ref| {
snapshot
.resolve_package_from_deno_module(package_ref.nv())
.ok()
});
if let Some(pkg) = maybe_package {
if let Some(module) = module.as_object_mut() {
module
.insert("npmPackage".to_string(), pkg.id.as_serialized().into());
}
}
} 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() {
if matches!(
modules[i].get("kind").and_then(|k| k.as_str()),
Some("npm") | Some("external")
) {
modules.remove(i);
}
}
}
for module in modules.iter_mut() {
if matches!(module.get("kind").and_then(|k| k.as_str()), Some("npm")) {
// 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 maybe_package = module
.get("specifier")
.and_then(|k| k.as_str())
.and_then(|specifier| NpmPackageNvReference::from_str(specifier).ok())
.and_then(|package_ref| {
snapshot
.resolve_package_from_deno_module(package_ref.nv())
.ok()
});
if let Some(pkg) = maybe_package {
if let Some(module) = module.as_object_mut() {
module
.insert("npmPackage".to_string(), pkg.id.as_serialized().into());
}
}
}
let dependencies = module
.get_mut("dependencies")
.and_then(|d| d.as_array_mut());
@ -265,7 +252,7 @@ fn add_npm_packages_to_json(
let mut json_packages = serde_json::Map::with_capacity(sorted_packages.len());
for pkg in sorted_packages {
let mut kv = serde_json::Map::new();
kv.insert("name".to_string(), pkg.id.nv.name.to_string().into());
kv.insert("name".to_string(), pkg.id.nv.name.clone().into());
kv.insert("version".to_string(), pkg.id.nv.version.to_string().into());
let mut deps = pkg.dependencies.values().collect::<Vec<_>>();
deps.sort();
@ -274,6 +261,8 @@ fn add_npm_packages_to_json(
.map(|id| serde_json::Value::String(id.as_serialized()))
.collect::<Vec<_>>();
kv.insert("dependencies".to_string(), deps.into());
let registry_url = npmrc.get_registry_url(&pkg.id.nv.name);
kv.insert("registryUrl".to_string(), registry_url.to_string().into());
json_packages.insert(pkg.id.as_serialized(), kv.into());
}

View file

@ -46,6 +46,16 @@
"size": 325,
"mediaType": "JavaScript",
"specifier": "[WILDCARD]/main.js"
},
{
"kind": "npm",
"specifier": "npm:/chai@4.3.6",
"npmPackage": "chai@4.3.6"
},
{
"kind": "npm",
"specifier": "npm:/chalk@4.1.2",
"npmPackage": "chalk@4.1.2"
}
],
"redirects": {
@ -58,12 +68,14 @@
"version": "4.3.0",
"dependencies": [
"color-convert@2.0.1"
]
],
"registryUrl": "http://localhost:4260/"
},
"assertion-error@1.1.0": {
"name": "assertion-error",
"version": "1.1.0",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
},
"chai@4.3.6": {
"name": "chai",
@ -76,7 +88,8 @@
"loupe@2.3.4",
"pathval@1.1.1",
"type-detect@4.0.8"
]
],
"registryUrl": "http://localhost:4260/"
},
"chalk@4.1.2": {
"name": "chalk",
@ -84,65 +97,76 @@
"dependencies": [
"ansi-styles@4.3.0",
"supports-color@7.2.0"
]
],
"registryUrl": "http://localhost:4260/"
},
"check-error@1.0.2": {
"name": "check-error",
"version": "1.0.2",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
},
"color-convert@2.0.1": {
"name": "color-convert",
"version": "2.0.1",
"dependencies": [
"color-name@1.1.4"
]
],
"registryUrl": "http://localhost:4260/"
},
"color-name@1.1.4": {
"name": "color-name",
"version": "1.1.4",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
},
"deep-eql@3.0.1": {
"name": "deep-eql",
"version": "3.0.1",
"dependencies": [
"type-detect@4.0.8"
]
],
"registryUrl": "http://localhost:4260/"
},
"get-func-name@2.0.0": {
"name": "get-func-name",
"version": "2.0.0",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
},
"has-flag@4.0.0": {
"name": "has-flag",
"version": "4.0.0",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
},
"loupe@2.3.4": {
"name": "loupe",
"version": "2.3.4",
"dependencies": [
"get-func-name@2.0.0"
]
],
"registryUrl": "http://localhost:4260/"
},
"pathval@1.1.1": {
"name": "pathval",
"version": "1.1.1",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
},
"supports-color@7.2.0": {
"name": "supports-color",
"version": "7.2.0",
"dependencies": [
"has-flag@4.0.0"
]
],
"registryUrl": "http://localhost:4260/"
},
"type-detect@4.0.8": {
"name": "type-detect",
"version": "4.0.8",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
}
}
}

View file

@ -46,6 +46,16 @@
"size": 325,
"mediaType": "JavaScript",
"specifier": "[WILDCARD]/main.js"
},
{
"kind": "npm",
"specifier": "npm:/chai@4.3.6",
"npmPackage": "chai@4.3.6"
},
{
"kind": "npm",
"specifier": "npm:/chalk@4.1.2",
"npmPackage": "chalk@4.1.2"
}
],
"redirects": {
@ -58,12 +68,14 @@
"version": "4.3.0",
"dependencies": [
"color-convert@2.0.1"
]
],
"registryUrl": "http://localhost:4260/"
},
"assertion-error@1.1.0": {
"name": "assertion-error",
"version": "1.1.0",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
},
"chai@4.3.6": {
"name": "chai",
@ -76,7 +88,8 @@
"loupe@2.3.4",
"pathval@1.1.1",
"type-detect@4.0.8"
]
],
"registryUrl": "http://localhost:4260/"
},
"chalk@4.1.2": {
"name": "chalk",
@ -84,65 +97,76 @@
"dependencies": [
"ansi-styles@4.3.0",
"supports-color@7.2.0"
]
],
"registryUrl": "http://localhost:4260/"
},
"check-error@1.0.2": {
"name": "check-error",
"version": "1.0.2",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
},
"color-convert@2.0.1": {
"name": "color-convert",
"version": "2.0.1",
"dependencies": [
"color-name@1.1.4"
]
],
"registryUrl": "http://localhost:4260/"
},
"color-name@1.1.4": {
"name": "color-name",
"version": "1.1.4",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
},
"deep-eql@3.0.1": {
"name": "deep-eql",
"version": "3.0.1",
"dependencies": [
"type-detect@4.0.8"
]
],
"registryUrl": "http://localhost:4260/"
},
"get-func-name@2.0.0": {
"name": "get-func-name",
"version": "2.0.0",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
},
"has-flag@4.0.0": {
"name": "has-flag",
"version": "4.0.0",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
},
"loupe@2.3.4": {
"name": "loupe",
"version": "2.3.4",
"dependencies": [
"get-func-name@2.0.0"
]
],
"registryUrl": "http://localhost:4260/"
},
"pathval@1.1.1": {
"name": "pathval",
"version": "1.1.1",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
},
"supports-color@7.2.0": {
"name": "supports-color",
"version": "7.2.0",
"dependencies": [
"has-flag@4.0.0"
]
],
"registryUrl": "http://localhost:4260/"
},
"type-detect@4.0.8": {
"name": "type-detect",
"version": "4.0.8",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
}
}
}

View file

@ -19,7 +19,8 @@
"version": "4.3.0",
"dependencies": [
"color-convert@2.0.1"
]
],
"registryUrl": "http://localhost:4260/"
},
"chalk@4.1.2": {
"name": "chalk",
@ -27,31 +28,36 @@
"dependencies": [
"ansi-styles@4.3.0",
"supports-color@7.2.0"
]
],
"registryUrl": "http://localhost:4260/"
},
"color-convert@2.0.1": {
"name": "color-convert",
"version": "2.0.1",
"dependencies": [
"color-name@1.1.4"
]
],
"registryUrl": "http://localhost:4260/"
},
"color-name@1.1.4": {
"name": "color-name",
"version": "1.1.4",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
},
"has-flag@4.0.0": {
"name": "has-flag",
"version": "4.0.0",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
},
"supports-color@7.2.0": {
"name": "supports-color",
"version": "7.2.0",
"dependencies": [
"has-flag@4.0.0"
]
],
"registryUrl": "http://localhost:4260/"
}
}
}

View file

@ -1,7 +1,7 @@
{
"version": 1,
"roots": [
"[WILDCARD]/peer_deps_with_copied_folders/main.ts"
"file://[WILDCARD]/main.ts"
],
"modules": [
{
@ -46,6 +46,16 @@
"size": 171,
"mediaType": "TypeScript",
"specifier": "file://[WILDCARD]/main.ts"
},
{
"kind": "npm",
"specifier": "npm:/@denotest/peer-dep-test-child@1.0.0",
"npmPackage": "@denotest/peer-dep-test-child@1.0.0_@denotest+peer-dep-test-peer@1.0.0"
},
{
"kind": "npm",
"specifier": "npm:/@denotest/peer-dep-test-child@2.0.0",
"npmPackage": "@denotest/peer-dep-test-child@2.0.0_@denotest+peer-dep-test-peer@2.0.0"
}
],
"redirects": {
@ -59,7 +69,8 @@
"dependencies": [
"@denotest/peer-dep-test-grandchild@1.0.0_@denotest+peer-dep-test-peer@1.0.0",
"@denotest/peer-dep-test-peer@1.0.0"
]
],
"registryUrl": "http://localhost:4260/"
},
"@denotest/peer-dep-test-child@2.0.0_@denotest+peer-dep-test-peer@2.0.0": {
"name": "@denotest/peer-dep-test-child",
@ -67,31 +78,36 @@
"dependencies": [
"@denotest/peer-dep-test-grandchild@1.0.0_@denotest+peer-dep-test-peer@2.0.0",
"@denotest/peer-dep-test-peer@2.0.0"
]
],
"registryUrl": "http://localhost:4260/"
},
"@denotest/peer-dep-test-grandchild@1.0.0_@denotest+peer-dep-test-peer@1.0.0": {
"name": "@denotest/peer-dep-test-grandchild",
"version": "1.0.0",
"dependencies": [
"@denotest/peer-dep-test-peer@1.0.0"
]
],
"registryUrl": "http://localhost:4260/"
},
"@denotest/peer-dep-test-grandchild@1.0.0_@denotest+peer-dep-test-peer@2.0.0": {
"name": "@denotest/peer-dep-test-grandchild",
"version": "1.0.0",
"dependencies": [
"@denotest/peer-dep-test-peer@2.0.0"
]
],
"registryUrl": "http://localhost:4260/"
},
"@denotest/peer-dep-test-peer@1.0.0": {
"name": "@denotest/peer-dep-test-peer",
"version": "1.0.0",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
},
"@denotest/peer-dep-test-peer@2.0.0": {
"name": "@denotest/peer-dep-test-peer",
"version": "2.0.0",
"dependencies": []
"dependencies": [],
"registryUrl": "http://localhost:4260/"
}
}
}

View file

@ -13,6 +13,10 @@
"run_node_modules_dir": {
"args": "run --node-modules-dir=auto -A --quiet main.js",
"output": "main.out"
},
"info": {
"args": "info --node-modules-dir=auto --json main.js",
"output": "info.out"
}
}
}

View file

@ -0,0 +1,87 @@
[UNORDERED_START]
Download http://localhost:4262/@denotest2/basic
Download http://localhost:4261/@denotest/basic
Download http://localhost:4261/@denotest/basic/1.0.0.tgz
Download http://localhost:4262/@denotest2/basic/1.0.0.tgz
[UNORDERED_END]
[UNORDERED_START]
Initialize @denotest/basic@1.0.0
Initialize @denotest2/basic@1.0.0
[UNORDERED_END]
{
"version": 1,
"roots": [
"file://[WILDCARD]/main.js"
],
"modules": [
{
"kind": "esm",
"dependencies": [
{
"specifier": "@denotest/basic",
"code": {
"specifier": "npm:@denotest/basic@1.0.0",
"span": {
"start": {
"line": 0,
"character": 35
},
"end": {
"line": 0,
"character": 52
}
}
}
},
{
"specifier": "@denotest2/basic",
"code": {
"specifier": "npm:@denotest2/basic@1.0.0",
"span": {
"start": {
"line": 1,
"character": 22
},
"end": {
"line": 1,
"character": 40
}
}
}
}
],
"local": "[WILDCARD]main.js",
"size": 192,
"mediaType": "JavaScript",
"specifier": "file://[WILDCARD]/main.js"
},
{
"kind": "npm",
"specifier": "npm:/@denotest/basic@1.0.0",
"npmPackage": "@denotest/basic@1.0.0"
},
{
"kind": "npm",
"specifier": "npm:/@denotest2/basic@1.0.0",
"npmPackage": "@denotest2/basic@1.0.0"
}
],
"redirects": {
"npm:@denotest/basic@1.0.0": "npm:/@denotest/basic@1.0.0",
"npm:@denotest2/basic@1.0.0": "npm:/@denotest2/basic@1.0.0"
},
"npmPackages": {
"@denotest/basic@1.0.0": {
"name": "@denotest/basic",
"version": "1.0.0",
"dependencies": [],
"registryUrl": "http://localhost:4261/"
},
"@denotest2/basic@1.0.0": {
"name": "@denotest2/basic",
"version": "1.0.0",
"dependencies": [],
"registryUrl": "http://localhost:4262/"
}
}
}