Skip to content

Commit 1329e9f

Browse files
committed
refactor(installer): simplify code after review
- Remove dirs_home() and dead fallback in resolve_install_dir(), use get_vp_home() directly - Resolve install dir once in run() and pass through to all functions - Extract read_current_version() into vite_setup as a public function, reuse in save_previous_version() and the installer - Merge Cli/Options structs into single Options struct in cli.rs - Extract replace_windows_exe() helper to eliminate copy-paste in setup_bin_shims() - Remove unused LPWSTR type alias from windows_path.rs - Remove excessive "Step N" comments - Fix mixed path separator in interactive menu display
1 parent aa4696e commit 1329e9f

File tree

4 files changed

+102
-168
lines changed

4 files changed

+102
-168
lines changed

crates/vite_installer/src/cli.rs

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,79 +5,65 @@ use clap::Parser;
55
/// Vite+ Installer — standalone installer for the vp CLI.
66
#[derive(Parser, Debug)]
77
#[command(name = "vp-setup", about = "Install the Vite+ CLI")]
8-
struct Cli {
8+
pub struct Options {
99
/// Accept defaults without prompting (for CI/unattended installs)
1010
#[arg(short = 'y', long = "yes")]
11-
yes: bool,
11+
pub yes: bool,
1212

1313
/// Suppress all output except errors
1414
#[arg(short = 'q', long = "quiet")]
15-
quiet: bool,
15+
pub quiet: bool,
1616

1717
/// Install a specific version (default: latest)
1818
#[arg(long = "version")]
19-
version: Option<String>,
19+
pub version: Option<String>,
2020

2121
/// npm dist-tag to install (default: latest)
2222
#[arg(long = "tag", default_value = "latest")]
23-
tag: String,
23+
pub tag: String,
2424

2525
/// Custom installation directory (default: ~/.vite-plus)
2626
#[arg(long = "install-dir")]
27-
install_dir: Option<String>,
27+
pub install_dir: Option<String>,
2828

2929
/// Custom npm registry URL
3030
#[arg(long = "registry")]
31-
registry: Option<String>,
31+
pub registry: Option<String>,
3232

3333
/// Skip Node.js version manager setup
3434
#[arg(long = "no-node-manager")]
35-
no_node_manager: bool,
35+
pub no_node_manager: bool,
3636

3737
/// Do not modify the User PATH
3838
#[arg(long = "no-modify-path")]
39-
no_modify_path: bool,
40-
}
41-
42-
/// Parsed installation options.
43-
pub struct Options {
44-
pub yes: bool,
45-
pub quiet: bool,
46-
pub version: Option<String>,
47-
pub tag: String,
48-
pub install_dir: Option<String>,
49-
pub registry: Option<String>,
50-
pub no_node_manager: bool,
5139
pub no_modify_path: bool,
5240
}
5341

5442
/// Parse CLI arguments, merging with environment variables.
55-
///
5643
/// CLI flags take precedence over environment variables.
5744
pub fn parse() -> Options {
58-
let cli = Cli::parse();
59-
60-
// Environment variable overrides (CLI flags take precedence)
61-
let version = cli.version.or_else(|| std::env::var("VP_VERSION").ok());
62-
let install_dir = cli.install_dir.or_else(|| std::env::var("VP_HOME").ok());
63-
let registry = cli.registry.or_else(|| std::env::var("NPM_CONFIG_REGISTRY").ok());
45+
let mut opts = Options::parse();
6446

65-
let no_node_manager = cli.no_node_manager
66-
|| std::env::var("VP_NODE_MANAGER")
47+
// Merge env var overrides (CLI flags already set take precedence)
48+
if opts.version.is_none() {
49+
opts.version = std::env::var("VP_VERSION").ok();
50+
}
51+
if opts.install_dir.is_none() {
52+
opts.install_dir = std::env::var("VP_HOME").ok();
53+
}
54+
if opts.registry.is_none() {
55+
opts.registry = std::env::var("NPM_CONFIG_REGISTRY").ok();
56+
}
57+
if !opts.no_node_manager {
58+
opts.no_node_manager = std::env::var("VP_NODE_MANAGER")
6759
.ok()
6860
.is_some_and(|v| v.eq_ignore_ascii_case("no"));
61+
}
6962

7063
// quiet implies yes
71-
let yes = cli.yes || cli.quiet;
72-
73-
Options {
74-
yes,
75-
quiet: cli.quiet,
76-
version,
77-
tag: cli.tag,
78-
install_dir,
79-
registry,
80-
no_node_manager,
81-
no_modify_path: cli.no_modify_path,
64+
if opts.quiet {
65+
opts.yes = true;
8266
}
67+
68+
opts
8369
}

0 commit comments

Comments
 (0)