Skip to content

Commit 8cdc794

Browse files
committed
fix(pm): allow registry commands to work outside a project
Commands like `vp info`, `vp pm search`, `vp pm ping`, and other registry/auth operations no longer require a package.json. When no project is found, these standalone commands fall back to a default npm package manager instance. Project-dependent commands (prune, pack, list, publish, rebuild, fund, audit) still require a package.json and show the existing "No package.json found." error. Closes #1108
1 parent 6749b17 commit 8cdc794

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

crates/vite_global_cli/src/commands/mod.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
2626
use std::{collections::HashMap, io::BufReader};
2727

28-
use vite_install::package_manager::PackageManager;
28+
use vite_install::package_manager::{PackageManager, PackageManagerType};
2929
use vite_path::AbsolutePath;
3030
use vite_shared::{PrependOptions, prepend_to_path_env};
3131

@@ -118,6 +118,37 @@ pub async fn build_package_manager(cwd: &AbsolutePath) -> Result<PackageManager,
118118
}
119119
}
120120

121+
/// Build a PackageManager, falling back to a default npm instance when no
122+
/// package.json is found. Uses `build()` instead of `build_with_default()`
123+
/// to skip the interactive package manager selection prompt on the fallback path.
124+
///
125+
/// Requires `prepend_js_runtime_to_path_env` to be called first so npm is on PATH.
126+
pub async fn build_package_manager_or_npm_default(
127+
cwd: &AbsolutePath,
128+
) -> Result<PackageManager, Error> {
129+
match PackageManager::builder(cwd).build().await {
130+
Ok(pm) => Ok(pm),
131+
Err(vite_error::Error::WorkspaceError(vite_workspace::Error::PackageJsonNotFound(_)))
132+
| Err(vite_error::Error::UnrecognizedPackageManager) => {
133+
Ok(default_npm_package_manager(cwd))
134+
}
135+
Err(e) => Err(e.into()),
136+
}
137+
}
138+
139+
fn default_npm_package_manager(cwd: &AbsolutePath) -> PackageManager {
140+
PackageManager {
141+
client: PackageManagerType::Npm,
142+
package_name: "npm".into(),
143+
version: "latest".into(),
144+
hash: None,
145+
bin_name: "npm".into(),
146+
workspace_root: cwd.to_absolute_path_buf(),
147+
is_monorepo: false,
148+
install_dir: cwd.to_absolute_path_buf(),
149+
}
150+
}
151+
121152
// Category A: Package manager commands
122153
pub mod add;
123154
pub mod dedupe;

crates/vite_global_cli/src/commands/pm.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ use vite_install::commands::{
2929
};
3030
use vite_path::AbsolutePathBuf;
3131

32-
use super::{build_package_manager, prepend_js_runtime_to_path_env};
32+
use super::{
33+
build_package_manager, build_package_manager_or_npm_default, prepend_js_runtime_to_path_env,
34+
};
3335
use crate::{
3436
cli::{ConfigCommands, DistTagCommands, OwnerCommands, PmCommands, TokenCommands},
3537
error::Error,
@@ -45,7 +47,7 @@ pub async fn execute_info(
4547
) -> Result<ExitStatus, Error> {
4648
prepend_js_runtime_to_path_env(&cwd).await?;
4749

48-
let package_manager = build_package_manager(&cwd).await?;
50+
let package_manager = build_package_manager_or_npm_default(&cwd).await?;
4951

5052
let options = ViewCommandOptions { package, field, json, pass_through_args };
5153

@@ -64,7 +66,23 @@ pub async fn execute_pm_subcommand(
6466

6567
prepend_js_runtime_to_path_env(&cwd).await?;
6668

67-
let package_manager = build_package_manager(&cwd).await?;
69+
// Project-dependent commands require package.json; standalone ones fall back to npm.
70+
let needs_project = matches!(
71+
command,
72+
PmCommands::Prune { .. }
73+
| PmCommands::Pack { .. }
74+
| PmCommands::List { .. }
75+
| PmCommands::Publish { .. }
76+
| PmCommands::Rebuild { .. }
77+
| PmCommands::Fund { .. }
78+
| PmCommands::Audit { .. }
79+
);
80+
81+
let package_manager = if needs_project {
82+
build_package_manager(&cwd).await?
83+
} else {
84+
build_package_manager_or_npm_default(&cwd).await?
85+
};
6886

6987
match command {
7088
PmCommands::Prune { prod, no_optional, pass_through_args } => {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
> vp info vite@2.0.0 version # should work without package.json
2+
2.0.0
3+
4+
> vp pm view vite@2.0.0 version # should work without package.json
5+
2.0.0
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"commands": [
3+
"vp info vite@2.0.0 version # should work without package.json",
4+
"vp pm view vite@2.0.0 version # should work without package.json"
5+
]
6+
}

0 commit comments

Comments
 (0)