Skip to content

Commit a40e9f4

Browse files
committed
FIXUP
1 parent 51ffca6 commit a40e9f4

33 files changed

Lines changed: 613 additions & 238 deletions

File tree

crates/vite_package_manager/src/package_manager.rs

Lines changed: 91 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ pub struct AddCommandOptions<'a> {
6767
pub save_dependency_type: Option<SaveDependencyType>,
6868
pub save_exact: bool,
6969
pub save_catalog_name: Option<&'a str>,
70-
pub filters: &'a [String],
70+
pub filters: Option<&'a [String]>,
7171
pub workspace_root: bool,
7272
pub workspace_only: bool,
7373
pub global: bool,
74-
pub pm_args: &'a [String],
74+
pub allow_build: Option<&'a str>,
75+
pub pass_through_args: Option<&'a [String]>,
7576
}
7677

7778
// TODO(@fengmk2): should move ResolveCommandResult to vite-common crate
@@ -247,8 +248,10 @@ impl PackageManager {
247248
bin_name = "npm".into();
248249
args.push("install".into());
249250
args.push("--global".into());
251+
if let Some(pass_through_args) = options.pass_through_args {
252+
args.extend_from_slice(pass_through_args);
253+
}
250254
args.extend_from_slice(options.packages);
251-
args.extend_from_slice(options.pm_args);
252255

253256
return ResolveCommandResult { bin_path: bin_name, args, envs };
254257
}
@@ -257,9 +260,11 @@ impl PackageManager {
257260
PackageManagerType::Pnpm => {
258261
bin_name = "pnpm".into();
259262
// pnpm: --filter must come before command
260-
for filter in options.filters {
261-
args.push("--filter".into());
262-
args.push(filter.clone());
263+
if let Some(filters) = options.filters {
264+
for filter in filters {
265+
args.push("--filter".into());
266+
args.push(filter.clone());
267+
}
263268
}
264269
args.push("add".into());
265270
if options.workspace_root {
@@ -294,19 +299,22 @@ impl PackageManager {
294299
if save_catalog_name.is_empty() {
295300
args.push("--save-catalog".into());
296301
} else {
297-
args.push("--save-catalog-name".into());
298-
args.push(save_catalog_name.into());
302+
args.push(format!("--save-catalog-name={}", save_catalog_name));
299303
}
300304
}
305+
306+
if let Some(allow_build) = options.allow_build {
307+
args.push(format!("--allow-build={}", allow_build));
308+
}
301309
}
302310
PackageManagerType::Yarn => {
303311
bin_name = "yarn".into();
304312
// FIXME: handle more than one workspace
305313
// yarn: workspace <pkg> add
306314
// https://yarnpkg.com/cli/workspace
307-
if !options.filters.is_empty() {
315+
if let Some(filters) = options.filters {
308316
args.push("workspace".into());
309-
args.push(options.filters[0].clone());
317+
args.push(filters[0].clone());
310318
}
311319
args.push("add".into());
312320
// FIXME: no workspace root flag for yarn
@@ -345,9 +353,11 @@ impl PackageManager {
345353
// npm: install --workspace <pkg>
346354
args.push("install".into());
347355
// FIXME: convert filter to workspace
348-
for filter in options.filters {
349-
args.push("--workspace".into());
350-
args.push(filter.clone());
356+
if let Some(filters) = options.filters {
357+
for filter in filters {
358+
args.push("--workspace".into());
359+
args.push(filter.clone());
360+
}
351361
}
352362
// https://docs.npmjs.com/cli/v11/commands/npm-install#include-workspace-root
353363
if options.workspace_root {
@@ -378,8 +388,10 @@ impl PackageManager {
378388
}
379389
}
380390

391+
if let Some(pass_through_args) = options.pass_through_args {
392+
args.extend_from_slice(pass_through_args);
393+
}
381394
args.extend_from_slice(options.packages);
382-
args.extend_from_slice(options.pm_args);
383395

384396
ResolveCommandResult { bin_path: bin_name, args, envs }
385397
}
@@ -2102,11 +2114,13 @@ mod tests {
21022114
packages: &["react".to_string()],
21032115
save_dependency_type: None,
21042116
save_exact: false,
2105-
filters: &[],
2117+
filters: None,
21062118
workspace_root: false,
21072119
workspace_only: false,
21082120
global: false,
21092121
save_catalog_name: None,
2122+
allow_build: None,
2123+
pass_through_args: None,
21102124
});
21112125
assert_eq!(result.bin_path, "pnpm");
21122126
assert_eq!(result.args, vec!["add", "react"]);
@@ -2119,11 +2133,13 @@ mod tests {
21192133
packages: &["react".to_string()],
21202134
save_dependency_type: None,
21212135
save_exact: false,
2122-
filters: &["app".to_string()],
2136+
filters: Some(&["app".to_string()]),
21232137
workspace_root: false,
21242138
workspace_only: false,
21252139
global: false,
21262140
save_catalog_name: None,
2141+
allow_build: None,
2142+
pass_through_args: None,
21272143
});
21282144
assert_eq!(result.args, vec!["--filter", "app", "add", "react"]);
21292145
assert_eq!(result.bin_path, "pnpm");
@@ -2136,15 +2152,17 @@ mod tests {
21362152
packages: &["react".to_string()],
21372153
save_dependency_type: None,
21382154
save_exact: false,
2139-
filters: &["app".to_string()],
2155+
filters: Some(&["app".to_string()]),
21402156
workspace_root: false,
21412157
workspace_only: false,
21422158
global: false,
21432159
save_catalog_name: Some("react18"),
2160+
allow_build: None,
2161+
pass_through_args: None,
21442162
});
21452163
assert_eq!(
21462164
result.args,
2147-
vec!["--filter", "app", "add", "--save-catalog-name", "react18", "react"]
2165+
vec!["--filter", "app", "add", "--save-catalog-name=react18", "react"]
21482166
);
21492167
assert_eq!(result.bin_path, "pnpm");
21502168
}
@@ -2156,11 +2174,13 @@ mod tests {
21562174
packages: &["react".to_string()],
21572175
save_dependency_type: None,
21582176
save_exact: false,
2159-
filters: &["app".to_string()],
2177+
filters: Some(&["app".to_string()]),
21602178
workspace_root: false,
21612179
workspace_only: false,
21622180
global: false,
21632181
save_catalog_name: Some(""),
2182+
allow_build: None,
2183+
pass_through_args: None,
21642184
});
21652185
assert_eq!(result.args, vec!["--filter", "app", "add", "--save-catalog", "react"]);
21662186
assert_eq!(result.bin_path, "pnpm");
@@ -2173,11 +2193,13 @@ mod tests {
21732193
packages: &["react".to_string()],
21742194
save_dependency_type: None,
21752195
save_exact: false,
2176-
filters: &["app".to_string()],
2196+
filters: Some(&["app".to_string()]),
21772197
workspace_root: true,
21782198
workspace_only: false,
21792199
global: false,
21802200
save_catalog_name: None,
2201+
allow_build: None,
2202+
pass_through_args: None,
21812203
});
21822204
assert_eq!(result.args, vec!["--filter", "app", "add", "--workspace-root", "react"]);
21832205
assert_eq!(result.bin_path, "pnpm");
@@ -2190,11 +2212,13 @@ mod tests {
21902212
packages: &["typescript".to_string()],
21912213
save_dependency_type: Some(SaveDependencyType::Dev),
21922214
save_exact: false,
2193-
filters: &[],
2215+
filters: None,
21942216
workspace_root: true,
21952217
workspace_only: false,
21962218
global: false,
21972219
save_catalog_name: None,
2220+
allow_build: None,
2221+
pass_through_args: None,
21982222
});
21992223
assert_eq!(result.args, vec!["add", "--workspace-root", "--save-dev", "typescript"]);
22002224
assert_eq!(result.bin_path, "pnpm");
@@ -2207,11 +2231,13 @@ mod tests {
22072231
packages: &["@myorg/utils".to_string()],
22082232
save_dependency_type: None,
22092233
save_exact: false,
2210-
filters: &["app".to_string()],
2234+
filters: Some(&["app".to_string()]),
22112235
workspace_root: false,
22122236
workspace_only: true,
22132237
global: false,
22142238
save_catalog_name: None,
2239+
allow_build: None,
2240+
pass_through_args: None,
22152241
});
22162242
assert_eq!(result.args, vec!["--filter", "app", "add", "--workspace", "@myorg/utils"]);
22172243
assert_eq!(result.bin_path, "pnpm");
@@ -2224,11 +2250,13 @@ mod tests {
22242250
packages: &["react".to_string()],
22252251
save_dependency_type: None,
22262252
save_exact: false,
2227-
filters: &[],
2253+
filters: None,
22282254
workspace_root: false,
22292255
workspace_only: false,
22302256
global: false,
22312257
save_catalog_name: None,
2258+
allow_build: None,
2259+
pass_through_args: None,
22322260
});
22332261
assert_eq!(result.args, vec!["add", "react"]);
22342262
assert_eq!(result.bin_path, "yarn");
@@ -2241,11 +2269,13 @@ mod tests {
22412269
packages: &["react".to_string()],
22422270
save_dependency_type: None,
22432271
save_exact: false,
2244-
filters: &["app".to_string()],
2272+
filters: Some(&["app".to_string()]),
22452273
workspace_root: false,
22462274
workspace_only: false,
22472275
global: false,
22482276
save_catalog_name: None,
2277+
allow_build: None,
2278+
pass_through_args: None,
22492279
});
22502280
assert_eq!(result.args, vec!["workspace", "app", "add", "react"]);
22512281
assert_eq!(result.bin_path, "yarn");
@@ -2258,11 +2288,13 @@ mod tests {
22582288
packages: &["typescript".to_string()],
22592289
save_dependency_type: Some(SaveDependencyType::Dev),
22602290
save_exact: false,
2261-
filters: &[],
2291+
filters: None,
22622292
workspace_root: true,
22632293
workspace_only: false,
22642294
global: false,
22652295
save_catalog_name: None,
2296+
allow_build: None,
2297+
pass_through_args: None,
22662298
});
22672299
// TODO: workspace-root not supported by yarn
22682300
assert_eq!(result.args, vec!["add", "--dev", "typescript"]);
@@ -2276,11 +2308,13 @@ mod tests {
22762308
packages: &["react".to_string()],
22772309
save_dependency_type: None,
22782310
save_exact: false,
2279-
filters: &[],
2311+
filters: None,
22802312
workspace_root: false,
22812313
workspace_only: false,
22822314
global: false,
22832315
save_catalog_name: None,
2316+
allow_build: None,
2317+
pass_through_args: None,
22842318
});
22852319
assert_eq!(result.args, vec!["install", "react"]);
22862320
assert_eq!(result.bin_path, "npm");
@@ -2293,11 +2327,13 @@ mod tests {
22932327
packages: &["react".to_string()],
22942328
save_dependency_type: None,
22952329
save_exact: false,
2296-
filters: &["app".to_string()],
2330+
filters: Some(&["app".to_string()]),
22972331
workspace_root: false,
22982332
workspace_only: false,
22992333
global: false,
23002334
save_catalog_name: None,
2335+
allow_build: None,
2336+
pass_through_args: None,
23012337
});
23022338
assert_eq!(result.args, vec!["install", "--workspace", "app", "react"]);
23032339
assert_eq!(result.bin_path, "npm");
@@ -2310,11 +2346,13 @@ mod tests {
23102346
packages: &["typescript".to_string()],
23112347
save_dependency_type: None,
23122348
save_exact: false,
2313-
filters: &[],
2349+
filters: None,
23142350
workspace_root: true,
23152351
workspace_only: false,
23162352
global: false,
23172353
save_catalog_name: None,
2354+
allow_build: None,
2355+
pass_through_args: None,
23182356
});
23192357
assert_eq!(result.args, vec!["install", "--include-workspace-root", "typescript"]);
23202358
assert_eq!(result.bin_path, "npm");
@@ -2327,11 +2365,13 @@ mod tests {
23272365
packages: &["lodash".to_string()],
23282366
save_dependency_type: None,
23292367
save_exact: false,
2330-
filters: &["app".to_string(), "web".to_string()],
2368+
filters: Some(&["app".to_string(), "web".to_string()]),
23312369
workspace_root: false,
23322370
workspace_only: false,
23332371
global: false,
23342372
save_catalog_name: None,
2373+
allow_build: None,
2374+
pass_through_args: None,
23352375
});
23362376
assert_eq!(
23372377
result.args,
@@ -2347,10 +2387,12 @@ mod tests {
23472387
packages: &["lodash".to_string()],
23482388
save_dependency_type: None,
23492389
save_exact: false,
2350-
filters: &["app".to_string(), "web".to_string()],
2390+
filters: Some(&["app".to_string(), "web".to_string()]),
23512391
workspace_root: true,
23522392
workspace_only: false,
23532393
global: false,
2394+
allow_build: None,
2395+
pass_through_args: None,
23542396
save_catalog_name: None,
23552397
});
23562398
assert_eq!(
@@ -2367,5 +2409,24 @@ mod tests {
23672409
);
23682410
assert_eq!(result.bin_path, "npm");
23692411
}
2412+
2413+
#[test]
2414+
fn test_pnpm_add_with_allow_build() {
2415+
let pm = create_mock_package_manager(PackageManagerType::Pnpm);
2416+
let result = pm.resolve_add_command(&AddCommandOptions {
2417+
packages: &["react".to_string()],
2418+
save_dependency_type: None,
2419+
save_exact: false,
2420+
filters: None,
2421+
workspace_root: false,
2422+
workspace_only: false,
2423+
global: false,
2424+
save_catalog_name: None,
2425+
allow_build: Some("react,napi"),
2426+
pass_through_args: None,
2427+
});
2428+
assert_eq!(result.args, vec!["add", "--allow-build=react,napi", "react"]);
2429+
assert_eq!(result.bin_path, "pnpm");
2430+
}
23702431
}
23712432
}

crates/vite_task/docs/rfc-add-remove-package-commands.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,20 @@ vite add react --filter app --filter web # Add to both app and web
205205
vite add react --filter "app*" --filter "!app-test" # Add to app* except app-test
206206
```
207207

208+
#### Pass-Through Arguments
209+
210+
Additional parameters not covered by Vite+ can all be handled through pass-through arguments.
211+
212+
All arguments after `--` will be passed through to the package manager.
213+
214+
```bash
215+
vite add react --allow-build=react,napi -- --use-stderr
216+
217+
-> pnpm add --allow-build=react,napi --use-stderr react
218+
-> yarn add --use-stderr react
219+
-> npm install --use-stderr react
220+
```
221+
208222
### Implementation Architecture
209223

210224
#### 1. Command Structure

0 commit comments

Comments
 (0)