Skip to content
Merged
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
33 changes: 32 additions & 1 deletion crates/vite_global_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

use std::{collections::HashMap, io::BufReader};

use vite_install::package_manager::PackageManager;
use vite_install::package_manager::{PackageManager, PackageManagerType};
use vite_path::AbsolutePath;
use vite_shared::{PrependOptions, prepend_to_path_env};

Expand Down Expand Up @@ -118,6 +118,37 @@ pub async fn build_package_manager(cwd: &AbsolutePath) -> Result<PackageManager,
}
}

/// Build a PackageManager, falling back to a default npm instance when no
/// package.json is found. Uses `build()` instead of `build_with_default()`
/// to skip the interactive package manager selection prompt on the fallback path.
///
/// Requires `prepend_js_runtime_to_path_env` to be called first so npm is on PATH.
pub async fn build_package_manager_or_npm_default(
cwd: &AbsolutePath,
) -> Result<PackageManager, Error> {
match PackageManager::builder(cwd).build().await {
Ok(pm) => Ok(pm),
Err(vite_error::Error::WorkspaceError(vite_workspace::Error::PackageJsonNotFound(_)))
| Err(vite_error::Error::UnrecognizedPackageManager) => {
Ok(default_npm_package_manager(cwd))
}
Err(e) => Err(e.into()),
}
}

fn default_npm_package_manager(cwd: &AbsolutePath) -> PackageManager {
PackageManager {
client: PackageManagerType::Npm,
package_name: "npm".into(),
version: "latest".into(),
hash: None,
bin_name: "npm".into(),
workspace_root: cwd.to_absolute_path_buf(),
is_monorepo: false,
install_dir: cwd.to_absolute_path_buf(),
}
}

// Category A: Package manager commands
pub mod add;
pub mod dedupe;
Expand Down
24 changes: 21 additions & 3 deletions crates/vite_global_cli/src/commands/pm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ use vite_install::commands::{
};
use vite_path::AbsolutePathBuf;

use super::{build_package_manager, prepend_js_runtime_to_path_env};
use super::{
build_package_manager, build_package_manager_or_npm_default, prepend_js_runtime_to_path_env,
};
use crate::{
cli::{ConfigCommands, DistTagCommands, OwnerCommands, PmCommands, TokenCommands},
error::Error,
Expand All @@ -45,7 +47,7 @@ pub async fn execute_info(
) -> Result<ExitStatus, Error> {
prepend_js_runtime_to_path_env(&cwd).await?;

let package_manager = build_package_manager(&cwd).await?;
let package_manager = build_package_manager_or_npm_default(&cwd).await?;

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

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

prepend_js_runtime_to_path_env(&cwd).await?;

let package_manager = build_package_manager(&cwd).await?;
// Project-dependent commands require package.json; standalone ones fall back to npm.
let needs_project = matches!(
command,
PmCommands::Prune { .. }
| PmCommands::Pack { .. }
| PmCommands::List { .. }
| PmCommands::Publish { .. }
| PmCommands::Rebuild { .. }
| PmCommands::Fund { .. }
| PmCommands::Audit { .. }
);

let package_manager = if needs_project {
build_package_manager(&cwd).await?
} else {
build_package_manager_or_npm_default(&cwd).await?
};

match command {
PmCommands::Prune { prod, no_optional, pass_through_args } => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
> vp info vite@2.0.0 version # should work without package.json
2.0.0

> vp pm view vite@2.0.0 version # should work without package.json
2.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"commands": [
"vp info vite@2.0.0 version # should work without package.json",
"vp pm view vite@2.0.0 version # should work without package.json"
]
}
Loading