diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_dlx_npm6/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_dlx_npm6/package.json new file mode 100644 index 0000000000..1ab6dd896e --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_dlx_npm6/package.json @@ -0,0 +1,5 @@ +{ + "name": "command-dlx-npm6", + "version": "1.0.0", + "packageManager": "npm@6.14.18" +} diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_dlx_npm6/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_dlx_npm6/snapshots.toml new file mode 100644 index 0000000000..09b1f6f0af --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_dlx_npm6/snapshots.toml @@ -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 }, +] diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_dlx_npm6/snapshots/command_dlx_npm6.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_dlx_npm6/snapshots/command_dlx_npm6.md new file mode 100644 index 0000000000..e77206dea4 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_dlx_npm6/snapshots/command_dlx_npm6.md @@ -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 +``` diff --git a/crates/vp_pm_cli/src/resolution/commands/dlx.rs b/crates/vp_pm_cli/src/resolution/commands/dlx.rs index 8efa787a4a..d7fc4d0df0 100644 --- a/crates/vp_pm_cli/src/resolution/commands/dlx.rs +++ b/crates/vp_pm_cli/src/resolution/commands/dlx.rs @@ -47,17 +47,24 @@ impl Resolve 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)); @@ -67,7 +74,8 @@ impl Resolve 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() } @@ -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( diff --git a/docs/guide/package-management/dlx.md b/docs/guide/package-management/dlx.md index 009425fc3e..6c94758ddc 100644 --- a/docs/guide/package-management/dlx.md +++ b/docs/guide/package-management/dlx.md @@ -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`.