Skip to content

Commit

Permalink
fix(install): support installing npm package with alias (#26246)
Browse files Browse the repository at this point in the history
Just tried this out today and it wasn't properly implemented in
#24156
  • Loading branch information
dsherret authored Oct 14, 2024
1 parent f3530c8 commit c5449d7
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 6 deletions.
12 changes: 11 additions & 1 deletion cli/tools/registry/pm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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('/', "__");
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions tests/specs/add/alias/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -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"
}]
}
4 changes: 4 additions & 0 deletions tests/specs/add/alias/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"dependencies": {
}
}
5 changes: 5 additions & 0 deletions tests/specs/add/alias/package.json.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"my-alias": "npm:@denotest/add@^1.0.0"
}
}
4 changes: 3 additions & 1 deletion tests/specs/add/dev/package.json.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"devDependencies": { "@denotest/esm-basic": "^1.0.0" }
"devDependencies": {
"@denotest/esm-basic": "^1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"dependencies": { "@denotest/esm-basic": "^1.0.0" }
"dependencies": {
"@denotest/esm-basic": "^1.0.0"
}
}
4 changes: 3 additions & 1 deletion tests/specs/install/install_add_dev/package.json.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"devDependencies": { "@denotest/esm-basic": "^1.0.0" }
"devDependencies": {
"@denotest/esm-basic": "^1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"dependencies": { "@denotest/deprecated-package": "^1.0.0" }
"dependencies": {
"@denotest/deprecated-package": "^1.0.0"
}
}
4 changes: 3 additions & 1 deletion tests/specs/remove/package_json/rm_add_package.json.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"devDependencies": { "@denotest/esm-basic": "^1.0.0" }
"devDependencies": {
"@denotest/esm-basic": "^1.0.0"
}
}

0 comments on commit c5449d7

Please sign in to comment.