Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "command-dlx-npm6",
"version": "1.0.0",
"packageManager": "npm@6.14.18"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[case]]
name = "command_dlx_npm6"
vp = "global"
local-registry = true
skip-platforms = ["windows", { os = "linux", libc = "musl" }]
env = { npm_config_audit = "false" }
comment = "npm 6 has no `npm exec`, so `vp dlx` runs its standalone npx."
steps = [
{ argv = ["vp", "dlx", "-s", "vp-test-echo", "hello", "--flag"], comment = "should run vp-test-echo with npx and pass its flags through", continue-on-failure = true },
{ argv = ["vp", "dlx", "-s", "vp-test-echo@1.0.0", "hello"], comment = "should run specific version", continue-on-failure = true },
{ argv = ["vp", "dlx", "-s", "-p", "vp-test-upper", "vp-test-echo@1.0.0", "hello"], comment = "should install the versioned command next to the extra package", continue-on-failure = true },
{ argv = ["vp", "dlx", "-s", "-p", "vp-test-echo", "-p", "vp-test-upper", "-c", "echo hello-from-shell | vp-test-echo | vp-test-upper"], comment = "should run shell mode command", continue-on-failure = true },
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# command_dlx_npm6

npm 6 has no `npm exec`, so `vp dlx` runs its standalone npx.

## `vp dlx -s vp-test-echo hello --flag`

should run vp-test-echo with npx and pass its flags through

```
hello --flag
```

## `vp dlx -s vp-test-echo@1.0.0 hello`

should run specific version

```
hello
```

## `vp dlx -s -p vp-test-upper vp-test-echo@1.0.0 hello`

should install the versioned command next to the extra package

```
hello
```

## `vp dlx -s -p vp-test-echo -p vp-test-upper -c 'echo hello-from-shell | vp-test-echo | vp-test-upper'`

should run shell mode command

```
HELLO-FROM-SHELL
```
53 changes: 48 additions & 5 deletions crates/vp_pm_cli/src/resolution/commands/dlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,24 @@ impl Resolve<DlxArgs> for Npm {
return CommandResolution::Noop;
};

let mut cmd = CommandBuilder::new("npm");
cmd.arg("exec");
// `npm exec` landed in npm 7. npm 6 only ships the standalone `npx`.
let has_exec = self.supports_v7_commands();
let mut cmd = CommandBuilder::new(if has_exec { "npm" } else { "npx" });
cmd.arg_if("exec", has_exec);
for package in &args.package {
cmd.arg(vt_str::format!("--package={package}"));
}
if !args.shell_mode && !args.package.is_empty() {
cmd.arg(vt_str::format!("--package={package_spec}"));
}
cmd.arg("--yes");
// npm 6 `npx` never prompts, and takes the word after an unknown `--yes` as its value.
cmd.arg_if("--yes", has_exec);
if args.silent {
cmd.arg("--loglevel").arg("silent");
if has_exec {
cmd.arg("--loglevel").arg("silent");
} else {
cmd.arg("--quiet");
}
}
if args.shell_mode {
cmd.arg("-c").arg(build_shell_command(package_spec, command_args));
Expand All @@ -67,7 +74,8 @@ impl Resolve<DlxArgs> for Npm {
} else {
extract_command_from_spec(package_spec)
};
cmd.arg("--").arg(command).extend(command_args);
// npm 6 `npx` already passes everything after the command through untouched.
cmd.arg_if("--", has_exec).arg(command).extend(command_args);
}
cmd.into()
}
Expand Down Expand Up @@ -330,6 +338,41 @@ mod tests {
);
}

#[test]
fn test_npm_6_uses_npx_without_exec_only_flags() {
let command = expect_run(
resolve(&npm("6.14.18"), dlx_args("create-vue@3.10.0", &["my-app"])).outcome,
);

assert_eq!(command.program, "npx");
assert_eq!(command.args, vec!["create-vue@3.10.0", "my-app"]);
}

#[test]
fn test_npm_6_npx_with_packages_and_silent() {
let mut options = dlx_args("yo@5.0.0", &["webapp"]);
options.package = vec!["generator-webapp".to_string()];
options.silent = true;
let command = expect_run(resolve(&npm("6.14.18"), options).outcome);

assert_eq!(command.program, "npx");
assert_eq!(
command.args,
vec!["--package=generator-webapp", "--package=yo@5.0.0", "--quiet", "yo", "webapp"]
);
}

#[test]
fn test_npm_6_npx_shell_mode() {
let mut options = dlx_args("echo hello | cowsay", &[]);
options.package = vec!["cowsay".to_string()];
options.shell_mode = true;
let command = expect_run(resolve(&npm("6.14.18"), options).outcome);

assert_eq!(command.program, "npx");
assert_eq!(command.args, vec!["--package=cowsay", "-c", "echo hello | cowsay"]);
}

#[test]
fn test_npm_exec_scoped_package_with_version() {
let command = expect_run(
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/package-management/dlx.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Arguments after the command are passed to the executed program, not parsed as Vi

## Compatibility

- **npm:** `npm exec --yes`.
- **npm:** `npm exec --yes`. npm 6 has no `npm exec`, so Vite+ runs its `npx`.
- **pnpm:** `pnpm dlx`.
- **Yarn Classic:** Fallback: `npx --yes`.
- **Yarn Modern:** `yarn dlx`.
Expand Down
Loading