From c5449d71da2d623e866d733b6db180a6f94ff7c6 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 14 Oct 2024 15:35:52 -0400 Subject: [PATCH] fix(install): support installing npm package with alias (#26246) Just tried this out today and it wasn't properly implemented in https://github.com/denoland/deno/pull/24156 --- cli/tools/registry/pm.rs | 12 +++++++++++- tests/specs/add/alias/__test__.jsonc | 13 +++++++++++++ tests/specs/add/alias/package.json | 4 ++++ tests/specs/add/alias/package.json.out | 5 +++++ tests/specs/add/dev/package.json.out | 4 +++- .../future_install_local_add_npm/package.json.out | 4 +++- .../specs/install/install_add_dev/package.json.out | 4 +++- .../install_deprecated_package/package.json.out | 4 +++- .../remove/package_json/rm_add_package.json.out | 4 +++- 9 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 tests/specs/add/alias/__test__.jsonc create mode 100644 tests/specs/add/alias/package.json create mode 100644 tests/specs/add/alias/package.json.out diff --git a/cli/tools/registry/pm.rs b/cli/tools/registry/pm.rs index 5dc0426202..3276acfbf7 100644 --- a/cli/tools/registry/pm.rs +++ b/cli/tools/registry/pm.rs @@ -363,7 +363,14 @@ fn package_json_dependency_entry( selected: SelectedPackage, ) -> (String, String) { if let Some(npm_package) = selected.package_name.strip_prefix("npm:") { - (npm_package.into(), selected.version_req) + if selected.import_name == npm_package { + (npm_package.into(), selected.version_req) + } else { + ( + selected.import_name, + format!("npm:{}@{}", npm_package, selected.version_req), + ) + } } else if let Some(jsr_package) = selected.package_name.strip_prefix("jsr:") { let jsr_package = jsr_package.strip_prefix('@').unwrap_or(jsr_package); let scope_replaced = jsr_package.replace('/', "__"); @@ -741,6 +748,9 @@ fn generate_imports(mut packages_to_version: Vec<(String, String)>) -> String { let mut contents = vec![]; let len = packages_to_version.len(); for (index, (package, version)) in packages_to_version.iter().enumerate() { + if index == 0 { + contents.push(String::new()); // force a newline at the start + } // TODO(bartlomieju): fix it, once we start support specifying version on the cli contents.push(format!("\"{}\": \"{}\"", package, version)); if index != len - 1 { diff --git a/tests/specs/add/alias/__test__.jsonc b/tests/specs/add/alias/__test__.jsonc new file mode 100644 index 0000000000..8a16ad3b46 --- /dev/null +++ b/tests/specs/add/alias/__test__.jsonc @@ -0,0 +1,13 @@ +{ + "tempDir": true, + "steps": [{ + "args": "install my-alias@npm:@denotest/add", + "output": "[WILDCARD]" + }, { + "args": [ + "eval", + "console.log(Deno.readTextFileSync('package.json').trim())" + ], + "output": "package.json.out" + }] +} diff --git a/tests/specs/add/alias/package.json b/tests/specs/add/alias/package.json new file mode 100644 index 0000000000..9664f260a7 --- /dev/null +++ b/tests/specs/add/alias/package.json @@ -0,0 +1,4 @@ +{ + "dependencies": { + } +} diff --git a/tests/specs/add/alias/package.json.out b/tests/specs/add/alias/package.json.out new file mode 100644 index 0000000000..b6326e8bfb --- /dev/null +++ b/tests/specs/add/alias/package.json.out @@ -0,0 +1,5 @@ +{ + "dependencies": { + "my-alias": "npm:@denotest/add@^1.0.0" + } +} diff --git a/tests/specs/add/dev/package.json.out b/tests/specs/add/dev/package.json.out index d5ca56e004..866724397f 100644 --- a/tests/specs/add/dev/package.json.out +++ b/tests/specs/add/dev/package.json.out @@ -1,3 +1,5 @@ { - "devDependencies": { "@denotest/esm-basic": "^1.0.0" } + "devDependencies": { + "@denotest/esm-basic": "^1.0.0" + } } diff --git a/tests/specs/install/future_install_local_add_npm/package.json.out b/tests/specs/install/future_install_local_add_npm/package.json.out index ad8518e791..613d986360 100644 --- a/tests/specs/install/future_install_local_add_npm/package.json.out +++ b/tests/specs/install/future_install_local_add_npm/package.json.out @@ -1,3 +1,5 @@ { - "dependencies": { "@denotest/esm-basic": "^1.0.0" } + "dependencies": { + "@denotest/esm-basic": "^1.0.0" + } } diff --git a/tests/specs/install/install_add_dev/package.json.out b/tests/specs/install/install_add_dev/package.json.out index d5ca56e004..866724397f 100644 --- a/tests/specs/install/install_add_dev/package.json.out +++ b/tests/specs/install/install_add_dev/package.json.out @@ -1,3 +1,5 @@ { - "devDependencies": { "@denotest/esm-basic": "^1.0.0" } + "devDependencies": { + "@denotest/esm-basic": "^1.0.0" + } } diff --git a/tests/specs/install/install_deprecated_package/package.json.out b/tests/specs/install/install_deprecated_package/package.json.out index 4b4b08087a..0cf36cd24f 100644 --- a/tests/specs/install/install_deprecated_package/package.json.out +++ b/tests/specs/install/install_deprecated_package/package.json.out @@ -1,3 +1,5 @@ { - "dependencies": { "@denotest/deprecated-package": "^1.0.0" } + "dependencies": { + "@denotest/deprecated-package": "^1.0.0" + } } diff --git a/tests/specs/remove/package_json/rm_add_package.json.out b/tests/specs/remove/package_json/rm_add_package.json.out index d5ca56e004..866724397f 100644 --- a/tests/specs/remove/package_json/rm_add_package.json.out +++ b/tests/specs/remove/package_json/rm_add_package.json.out @@ -1,3 +1,5 @@ { - "devDependencies": { "@denotest/esm-basic": "^1.0.0" } + "devDependencies": { + "@denotest/esm-basic": "^1.0.0" + } }