Skip to content

Commit c0e7912

Browse files
committed
fix(cli): support chained argument normalization
- Refactor `normalize_args` to run in a loop, allowing multiple normalizations to be applied sequentially. - This fixes issues where commands like `vp help rebuild` or `vp help node` were not fully normalized to their target commands. - Update global CLI snapshot tests to reflect the new `rebuild` top-level command in help output.
1 parent f8a1029 commit c0e7912

2 files changed

Lines changed: 65 additions & 44 deletions

File tree

  • crates/vite_global_cli/src
  • packages/cli/snap-tests-global/cli-helper-message

crates/vite_global_cli/src/main.rs

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -40,49 +40,56 @@ use crate::cli::{
4040
/// - `vp help [command]` → `vp [command] --help`
4141
/// - `vp node [args...]` → `vp env exec node [args...]`
4242
fn normalize_args(args: Vec<String>) -> Vec<String> {
43-
match args.get(1).map(String::as_str) {
44-
// `vp list ...` → `vp pm list ...`
45-
// `vp ls ...` → `vp pm list ...`
46-
Some("list" | "ls") => {
47-
let mut normalized = Vec::with_capacity(args.len() + 1);
48-
normalized.push(args[0].clone());
49-
normalized.push("pm".to_string());
50-
normalized.push("list".to_string());
51-
normalized.extend(args[2..].iter().cloned());
52-
normalized
53-
}
54-
// `vp rebuild ...` → `vp pm rebuild ...`
55-
Some("rebuild") => {
56-
let mut normalized = Vec::with_capacity(args.len() + 1);
57-
normalized.push(args[0].clone());
58-
normalized.push("pm".to_string());
59-
normalized.push("rebuild".to_string());
60-
normalized.extend(args[2..].iter().cloned());
61-
normalized
62-
}
63-
// `vp help` alone -> show main help
64-
Some("help") if args.len() == 2 => vec![args[0].clone(), "--help".to_string()],
65-
// `vp help [command] [args...]` -> `vp [command] --help [args...]`
66-
Some("help") if args.len() > 2 => {
67-
let mut normalized = Vec::with_capacity(args.len());
68-
normalized.push(args[0].clone());
69-
normalized.push(args[2].clone());
70-
normalized.push("--help".to_string());
71-
normalized.extend(args[3..].iter().cloned());
72-
normalized
73-
}
74-
// `vp node [args...]` → `vp env exec node [args...]`
75-
Some("node") => {
76-
let mut normalized = Vec::with_capacity(args.len() + 2);
77-
normalized.push(args[0].clone());
78-
normalized.push("env".to_string());
79-
normalized.push("exec".to_string());
80-
normalized.push("node".to_string());
81-
normalized.extend(args[2..].iter().cloned());
82-
normalized
83-
}
84-
// No transformation needed
85-
_ => args,
43+
let mut normalized = args;
44+
loop {
45+
let next = match normalized.get(1).map(String::as_str) {
46+
// `vp list ...` → `vp pm list ...`
47+
// `vp ls ...` → `vp pm list ...`
48+
Some("list" | "ls") => {
49+
let mut next = Vec::with_capacity(normalized.len() + 1);
50+
next.push(normalized[0].clone());
51+
next.push("pm".to_string());
52+
next.push("list".to_string());
53+
next.extend(normalized[2..].iter().cloned());
54+
next
55+
}
56+
// `vp rebuild ...` → `vp pm rebuild ...`
57+
Some("rebuild") => {
58+
let mut next = Vec::with_capacity(normalized.len() + 1);
59+
next.push(normalized[0].clone());
60+
next.push("pm".to_string());
61+
next.push("rebuild".to_string());
62+
next.extend(normalized[2..].iter().cloned());
63+
next
64+
}
65+
// `vp help` alone -> show main help
66+
Some("help") if normalized.len() == 2 => {
67+
vec![normalized[0].clone(), "--help".to_string()]
68+
}
69+
// `vp help [command] [args...]` -> `vp [command] --help [args...]`
70+
Some("help") if normalized.len() > 2 => {
71+
let mut next = Vec::with_capacity(normalized.len());
72+
next.push(normalized[0].clone());
73+
next.push(normalized[2].clone());
74+
next.push("--help".to_string());
75+
next.extend(normalized[3..].iter().cloned());
76+
next
77+
}
78+
// `vp node [args...]` → `vp env exec node [args...]`
79+
Some("node") => {
80+
let mut next = Vec::with_capacity(normalized.len() + 2);
81+
next.push(normalized[0].clone());
82+
next.push("env".to_string());
83+
next.push("exec".to_string());
84+
next.push("node".to_string());
85+
next.extend(normalized[2..].iter().cloned());
86+
next
87+
}
88+
// No transformation needed
89+
_ => return normalized,
90+
};
91+
92+
normalized = next;
8693
}
8794
}
8895

@@ -458,6 +465,20 @@ mod tests {
458465
assert_eq!(normalized, s(&["vp", "pm", "rebuild", "--", "--update-binary"]));
459466
}
460467

468+
#[test]
469+
fn normalize_args_rewrites_vp_help_rebuild() {
470+
let input = s(&["vp", "help", "rebuild"]);
471+
let normalized = normalize_args(input);
472+
assert_eq!(normalized, s(&["vp", "pm", "rebuild", "--help"]));
473+
}
474+
475+
#[test]
476+
fn normalize_args_rewrites_vp_help_node() {
477+
let input = s(&["vp", "help", "node"]);
478+
let normalized = normalize_args(input);
479+
assert_eq!(normalized, s(&["vp", "env", "exec", "node", "--help"]));
480+
}
481+
461482
#[test]
462483
fn unknown_argument_detected_without_pass_as_value_hint() {
463484
let error = try_parse_args_from(["vp".to_string(), "--cache".to_string()])

packages/cli/snap-tests-global/cli-helper-message/snap.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Manage Dependencies:
4141
info, view, show View package information from the registry
4242
link, ln Link packages for local development
4343
unlink Unlink packages
44+
rebuild Rebuild native modules
4445
pm Forward a command to the package manager
4546

4647
Maintain:
@@ -441,4 +442,3 @@ Options:
441442
-h, --help Print help
442443

443444
Documentation: https://viteplus.dev/guide/upgrade
444-

0 commit comments

Comments
 (0)