-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathmain.rs
More file actions
428 lines (373 loc) · 15.3 KB
/
main.rs
File metadata and controls
428 lines (373 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Vite+ Global CLI
//!
//! A standalone Rust binary for the vite+ global CLI that can run without
//! pre-installed Node.js. Uses managed Node.js from `vite_js_runtime` for
//! package manager commands and JS script execution.
// Allow printing to stderr for CLI error messages
#![allow(clippy::print_stderr)]
mod cli;
mod command_picker;
mod commands;
mod error;
mod help;
mod js_executor;
mod shim;
mod tips;
use std::{
io::{IsTerminal, Write},
process::{ExitCode, ExitStatus},
};
use clap::error::{ContextKind, ContextValue};
use owo_colors::OwoColorize;
use vite_shared::output;
pub use crate::cli::try_parse_args_from;
use crate::cli::{
RenderOptions, run_command, run_command_with_options, try_parse_args_from_with_options,
};
/// Normalize CLI arguments:
/// - `vp list ...` / `vp ls ...` → `vp pm list ...`
/// - `vp help [command]` → `vp [command] --help`
fn normalize_args(args: Vec<String>) -> Vec<String> {
match args.get(1).map(String::as_str) {
// `vp list ...` → `vp pm list ...`
// `vp ls ...` → `vp pm list ...`
Some("list" | "ls") => {
let mut normalized = Vec::with_capacity(args.len() + 1);
normalized.push(args[0].clone());
normalized.push("pm".to_string());
normalized.push("list".to_string());
normalized.extend(args[2..].iter().cloned());
normalized
}
// `vp help` alone -> show main help
Some("help") if args.len() == 2 => vec![args[0].clone(), "--help".to_string()],
// `vp help [command] [args...]` -> `vp [command] --help [args...]`
Some("help") if args.len() > 2 => {
let mut normalized = Vec::with_capacity(args.len());
normalized.push(args[0].clone());
normalized.push(args[2].clone());
normalized.push("--help".to_string());
normalized.extend(args[3..].iter().cloned());
normalized
}
// No transformation needed
_ => args,
}
}
struct InvalidSubcommandDetails {
invalid_subcommand: String,
suggestion: Option<String>,
}
fn extract_invalid_subcommand_details(error: &clap::Error) -> Option<InvalidSubcommandDetails> {
let invalid_subcommand = match error.get(ContextKind::InvalidSubcommand) {
Some(ContextValue::String(value)) => value.as_str(),
_ => return None,
};
let suggestion = match error.get(ContextKind::SuggestedSubcommand) {
Some(ContextValue::String(value)) => Some(value.to_owned()),
Some(ContextValue::Strings(values)) => {
vite_shared::string_similarity::pick_best_suggestion(invalid_subcommand, values)
}
_ => None,
};
Some(InvalidSubcommandDetails { invalid_subcommand: invalid_subcommand.to_owned(), suggestion })
}
fn print_invalid_subcommand_error(details: &InvalidSubcommandDetails) {
println!("{}", vite_shared::header::vite_plus_header());
println!();
let highlighted_subcommand = details.invalid_subcommand.bright_blue().to_string();
output::error(&format!("Command '{highlighted_subcommand}' not found"));
}
fn is_affirmative_response(input: &str) -> bool {
matches!(input.trim().to_ascii_lowercase().as_str(), "y" | "yes" | "ok" | "true" | "1")
}
fn should_prompt_for_correction() -> bool {
std::io::stdin().is_terminal() && std::io::stderr().is_terminal()
}
fn prompt_to_run_suggested_command(suggestion: &str) -> bool {
if !should_prompt_for_correction() {
return false;
}
eprintln!();
let highlighted_suggestion = format!("`vp {suggestion}`").bright_blue().to_string();
eprint!("Do you want to run {highlighted_suggestion}? (y/N): ");
if std::io::stderr().flush().is_err() {
return false;
}
let Some(input) = read_confirmation_input() else {
return false;
};
is_affirmative_response(input.trim())
}
fn read_confirmation_input() -> Option<String> {
let mut input = String::new();
std::io::stdin().read_line(&mut input).ok()?;
Some(input)
}
fn replace_top_level_typoed_subcommand(
raw_args: &[String],
invalid_subcommand: &str,
suggestion: &str,
) -> Option<Vec<String>> {
let index = raw_args.iter().position(|arg| !arg.starts_with('-'))?;
if raw_args.get(index)? != invalid_subcommand {
return None;
}
let mut corrected = raw_args.to_vec();
corrected[index] = suggestion.to_owned();
Some(corrected)
}
fn exit_status_to_exit_code(exit_status: ExitStatus) -> ExitCode {
if exit_status.success() {
ExitCode::SUCCESS
} else {
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
exit_status.code().map_or(ExitCode::FAILURE, |c| ExitCode::from(c as u8))
}
}
async fn run_corrected_args(cwd: &vite_path::AbsolutePathBuf, raw_args: &[String]) -> ExitCode {
let render_options = RenderOptions { show_header: false };
let args_with_program = std::iter::once("vp".to_string()).chain(raw_args.iter().cloned());
let normalized_args = normalize_args(args_with_program.collect());
let parsed = match try_parse_args_from_with_options(normalized_args, render_options) {
Ok(args) => args,
Err(e) => {
e.print().ok();
#[allow(clippy::cast_sign_loss)]
return ExitCode::from(e.exit_code() as u8);
}
};
match run_command_with_options(cwd.clone(), parsed, render_options).await {
Ok(exit_status) => exit_status_to_exit_code(exit_status),
Err(e) => {
if matches!(&e, error::Error::UserMessage(_)) {
eprintln!("{e}");
} else {
output::error(&format!("{e}"));
}
ExitCode::FAILURE
}
}
}
fn extract_unknown_argument(error: &clap::Error) -> Option<String> {
match error.get(ContextKind::InvalidArg) {
Some(ContextValue::String(value)) => Some(value.to_owned()),
_ => None,
}
}
fn has_pass_as_value_suggestion(error: &clap::Error) -> bool {
let contains_pass_as_value = |suggestion: &str| suggestion.contains("as a value");
match error.get(ContextKind::Suggested) {
Some(ContextValue::String(suggestion)) => contains_pass_as_value(suggestion),
Some(ContextValue::Strings(suggestions)) => {
suggestions.iter().any(|suggestion| contains_pass_as_value(suggestion))
}
Some(ContextValue::StyledStr(suggestion)) => {
contains_pass_as_value(&suggestion.to_string())
}
Some(ContextValue::StyledStrs(suggestions)) => {
suggestions.iter().any(|suggestion| contains_pass_as_value(&suggestion.to_string()))
}
_ => false,
}
}
fn print_unknown_argument_error(error: &clap::Error) -> bool {
let Some(invalid_argument) = extract_unknown_argument(error) else {
return false;
};
println!("{}", vite_shared::header::vite_plus_header());
println!();
let highlighted_argument = invalid_argument.bright_blue().to_string();
output::error(&format!("Unexpected argument '{highlighted_argument}'"));
if has_pass_as_value_suggestion(error) {
eprintln!();
let pass_through_argument = format!("-- {invalid_argument}");
let highlighted_pass_through_argument =
format!("`{}`", pass_through_argument.bright_blue());
eprintln!("Use {highlighted_pass_through_argument} to pass the argument as a value");
}
true
}
#[tokio::main]
async fn main() -> ExitCode {
// Initialize tracing
vite_shared::init_tracing();
// Check for shim mode (invoked as node, npm, or npx)
let mut args: Vec<String> = std::env::args().collect();
let argv0 = args.first().map(|s| s.as_str()).unwrap_or("vp");
tracing::debug!("argv0: {argv0}");
if let Some(tool) = shim::detect_shim_tool(argv0) {
// Shim mode - dispatch to the appropriate tool
let exit_code = shim::dispatch(&tool, &args[1..]).await;
return ExitCode::from(exit_code as u8);
}
// Normal CLI mode - get current working directory
let cwd = match vite_path::current_dir() {
Ok(path) => path,
Err(e) => {
output::error(&format!("Failed to get current directory: {e}"));
return ExitCode::FAILURE;
}
};
// Set terminal title to the project name from package.json
if let Some(project_name) = vite_shared::read_project_name(cwd.as_path().as_ref()) {
vite_shared::header::set_terminal_title(&project_name);
}
if args.len() == 1 {
match command_picker::pick_top_level_command_if_interactive(&cwd) {
Ok(command_picker::TopLevelCommandPick::Selected(selection)) => {
args.push(selection.command.to_string());
if selection.append_help {
args.push("--help".to_string());
}
}
Ok(command_picker::TopLevelCommandPick::Cancelled) => {
return ExitCode::SUCCESS;
}
Ok(command_picker::TopLevelCommandPick::Skipped) => {}
Err(err) => {
tracing::debug!("Failed to run top-level command picker: {err}");
}
}
}
let mut tip_context = tips::TipContext {
// Capture user args (excluding argv0) before normalization
raw_args: args[1..].to_vec(),
..Default::default()
};
// Normalize arguments (list/ls aliases, help rewriting)
let normalized_args = normalize_args(args);
// Print unified subcommand help for clap-managed commands before clap handles help output.
if help::maybe_print_unified_clap_subcommand_help(&normalized_args) {
return ExitCode::SUCCESS;
}
// Parse CLI arguments (using custom help formatting)
let exit_code = match try_parse_args_from(normalized_args) {
Err(e) => {
use clap::error::ErrorKind;
// --help and --version are clap "errors" but should exit successfully.
if matches!(e.kind(), ErrorKind::DisplayHelp | ErrorKind::DisplayVersion) {
e.print().ok();
ExitCode::SUCCESS
} else if matches!(e.kind(), ErrorKind::InvalidSubcommand) {
if let Some(details) = extract_invalid_subcommand_details(&e) {
print_invalid_subcommand_error(&details);
if let Some(suggestion) = &details.suggestion {
if let Some(corrected_raw_args) = replace_top_level_typoed_subcommand(
&tip_context.raw_args,
&details.invalid_subcommand,
suggestion,
) {
if prompt_to_run_suggested_command(suggestion) {
tip_context.raw_args = corrected_raw_args.clone();
run_corrected_args(&cwd, &corrected_raw_args).await
} else {
let code = e.exit_code();
tip_context.clap_error = Some(e);
#[allow(clippy::cast_sign_loss)]
ExitCode::from(code as u8)
}
} else {
let code = e.exit_code();
tip_context.clap_error = Some(e);
#[allow(clippy::cast_sign_loss)]
ExitCode::from(code as u8)
}
} else {
let code = e.exit_code();
tip_context.clap_error = Some(e);
#[allow(clippy::cast_sign_loss)]
ExitCode::from(code as u8)
}
} else {
e.print().ok();
let code = e.exit_code();
tip_context.clap_error = Some(e);
#[allow(clippy::cast_sign_loss)]
ExitCode::from(code as u8)
}
} else if matches!(e.kind(), ErrorKind::UnknownArgument) {
if !print_unknown_argument_error(&e) {
e.print().ok();
}
let code = e.exit_code();
tip_context.clap_error = Some(e);
#[allow(clippy::cast_sign_loss)]
ExitCode::from(code as u8)
} else {
e.print().ok();
let code = e.exit_code();
tip_context.clap_error = Some(e);
#[allow(clippy::cast_sign_loss)]
ExitCode::from(code as u8)
}
}
Ok(args) => match run_command(cwd.clone(), args).await {
Ok(exit_status) => exit_status_to_exit_code(exit_status),
Err(e) => {
if matches!(&e, error::Error::UserMessage(_)) {
eprintln!("{e}");
} else {
output::error(&format!("{e}"));
}
ExitCode::FAILURE
}
},
};
tip_context.exit_code = if exit_code == ExitCode::SUCCESS { 0 } else { 1 };
if let Some(tip) = tips::get_tip(&tip_context) {
eprintln!("\n{} {}", "tip:".bright_black().bold(), tip.bright_black());
}
exit_code
}
#[cfg(test)]
mod tests {
use clap::error::ErrorKind;
use super::{
extract_unknown_argument, has_pass_as_value_suggestion, is_affirmative_response,
replace_top_level_typoed_subcommand, try_parse_args_from,
};
#[test]
fn unknown_argument_detected_without_pass_as_value_hint() {
let error = try_parse_args_from(["vp".to_string(), "--cache".to_string()])
.expect_err("Expected parse error");
assert_eq!(error.kind(), ErrorKind::UnknownArgument);
assert_eq!(extract_unknown_argument(&error).as_deref(), Some("--cache"));
assert!(!has_pass_as_value_suggestion(&error));
}
#[test]
fn unknown_argument_detected_with_pass_as_value_hint() {
let error = try_parse_args_from([
"vp".to_string(),
"remove".to_string(),
"--stream".to_string(),
"foo".to_string(),
])
.expect_err("Expected parse error");
assert_eq!(error.kind(), ErrorKind::UnknownArgument);
assert_eq!(extract_unknown_argument(&error).as_deref(), Some("--stream"));
assert!(has_pass_as_value_suggestion(&error));
}
#[test]
fn affirmative_response_detection() {
assert!(is_affirmative_response("y"));
assert!(is_affirmative_response("yes"));
assert!(is_affirmative_response("Y"));
assert!(!is_affirmative_response("Sure"));
assert!(!is_affirmative_response("n"));
assert!(!is_affirmative_response(""));
}
#[test]
fn replace_top_level_typoed_subcommand_preserves_trailing_args() {
let raw_args = vec!["fnt".to_string(), "--write".to_string(), "src".to_string()];
let corrected = replace_top_level_typoed_subcommand(&raw_args, "fnt", "fmt")
.expect("Expected typoed command to be replaced");
assert_eq!(corrected, vec!["fmt".to_string(), "--write".to_string(), "src".to_string()]);
}
#[test]
fn replace_top_level_typoed_subcommand_skips_nested_subcommands() {
let raw_args = vec!["env".to_string(), "typo".to_string()];
let corrected = replace_top_level_typoed_subcommand(&raw_args, "typo", "on");
assert!(corrected.is_none());
}
}