Skip to content

Commit e687c62

Browse files
authored
feat(pm): vite add command for package installation (#221)
### TL;DR Added a new `vite add` command to simplify adding packages to dependencies with automatic package manager detection. ### What changed? - Added a new `AddCommand` implementation in `vite_task/src/add.rs` that handles adding packages to dependencies - Implemented `build_add_args` method in `PackageManager` to generate the correct command arguments for different package managers (npm, yarn, pnpm) - Added support for workspace filtering, workspace root, and workspace-only flags - Added new error types for package manager detection and package specification - Added comprehensive tests for the new functionality ### How to test? Test the new command with different package managers: ``` # Basic usage vite add react react-dom # Add dev dependencies vite add typescript -D # Add to specific workspace packages vite add lodash --filter app # Add to workspace root vite add eslint -w # Add to multiple workspaces vite add jest --filter app --filter web ``` ### Why make this change? This change simplifies the process of adding dependencies to projects by automatically detecting the package manager and translating the command to the appropriate syntax. It eliminates the need for users to remember different command formats for npm, yarn, and pnpm, especially in monorepo setups where workspace flags differ significantly between package managers. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Introduces a unified `vite add` command that detects pnpm/yarn/npm, builds correct args (filters, workspace/root/global, save flags), aliases `install <pkgs>` to add, and adds error handling with extensive tests/snapshots. > - **CLI (vite task)**: > - Add `Add` subcommand in `crates/vite_task/src/lib.rs` with flags (`-D/-P/-O/--save-peer`, `-E`, `--filter`, `-w`, `--workspace`, `-g`, pass-through `--`). > - Implement execution in `crates/vite_task/src/add.rs`; prints resolved command and runs via task graph. > - Treat `install <PACKAGES>` as alias to `add` via `parse_install_as_add`. > - **Package Manager Adapter**: > - New `crates/vite_package_manager/src/add.rs` with `AddCommandOptions` and `SaveDependencyType` and `PackageManager::resolve_add_command` that maps to pnpm/yarn/npm args (filters ordering, workspace/root, save flags, exact, catalogs, `--allow-build`, global via npm). > - Export in `crates/vite_package_manager/src/lib.rs`. > - **Errors**: > - Add `Error::NoPackagesSpecified` in `crates/vite_error/src/lib.rs`. > - **Tests/Fixtures**: > - Comprehensive unit tests for arg resolution and command building across pnpm (v9/10), yarn (v4), npm (v10/11), including workspaces, catalogs, pass-through, and global. > - Update CLI help/snapshots to include `add` and new outputs. > - `packages/tools`: normalize CI output; strip Yarn `YN0013` in `replaceUnstableOutput` with new test. > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit b868089. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 4aeb13e commit e687c62

64 files changed

Lines changed: 4898 additions & 30 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/vite_error/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ pub enum Error {
133133
#[error("Unrecognized any package manager, please specify the package manager")]
134134
UnrecognizedPackageManager,
135135

136+
#[error("No packages specified. Usage: vite add <packages>...")]
137+
NoPackagesSpecified,
138+
136139
#[error(
137140
"Package manager {name}@{version} in {package_json_path:?} is invalid, expected format: 'package-manager-name@major.minor.patch'"
138141
)]

crates/vite_package_manager/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ reqwest = { workspace = true, features = ["stream", "native-tls-vendored", "json
4141

4242
[target.'cfg(not(target_os = "windows"))'.dependencies]
4343
reqwest = { workspace = true, features = ["stream", "rustls-tls", "json"] }
44+
nix = { workspace = true }
4445

4546
[dev-dependencies]
4647
httpmock = { workspace = true }

0 commit comments

Comments
 (0)