Skip to content

Commit 1698430

Browse files
committed
refactor: extract VP_BINARY_NAME const and fix review findings
- Extract VP_BINARY_NAME const to vite_setup::lib.rs, replacing 5 inline cfg!(windows) expressions across vite_setup, vite_installer, and vite_global_cli - Fix create_env_files to check exit code and log warning on failure, matching refresh_shims behavior - Fix else { if let → else if let (clippy collapsible_else_if)
1 parent 4c1d7aa commit 1698430

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

crates/vite_global_cli/src/commands/upgrade/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ async fn install_platform_and_main(
147147
install::extract_platform_package(platform_data, version_dir).await?;
148148

149149
// Verify binary was extracted
150-
let binary_name = if cfg!(windows) { "vp.exe" } else { "vp" };
150+
let binary_name = vite_setup::VP_BINARY_NAME;
151151
let binary_path = version_dir.join("bin").join(binary_name);
152152
if !tokio::fs::try_exists(&binary_path).await.unwrap_or(false) {
153153
return Err(Error::Upgrade(

crates/vite_installer/src/main.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ use indicatif::{ProgressBar, ProgressStyle};
2020
use owo_colors::OwoColorize;
2121
use vite_install::request::HttpClient;
2222
use vite_path::AbsolutePathBuf;
23-
use vite_setup::{install, integrity, platform, registry};
24-
25-
const VP_BINARY_NAME: &str = if cfg!(windows) { "vp.exe" } else { "vp" };
23+
use vite_setup::{VP_BINARY_NAME, install, integrity, platform, registry};
2624

2725
/// Restrict DLL search to system32 only to prevent DLL hijacking
2826
/// when the installer is run from a Downloads folder.
@@ -206,10 +204,8 @@ async fn do_install(
206204
if let Err(e) = install::refresh_shims(install_dir).await {
207205
print_warn(&format!("Node.js manager setup failed (non-fatal): {e}"));
208206
}
209-
} else {
210-
if let Err(e) = install::create_env_files(install_dir).await {
211-
print_warn(&format!("Env file creation failed (non-fatal): {e}"));
212-
}
207+
} else if let Err(e) = install::create_env_files(install_dir).await {
208+
print_warn(&format!("Env file creation failed (non-fatal): {e}"));
213209
}
214210

215211
if !opts.no_modify_path {

crates/vite_setup/src/install.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ pub async fn install_production_deps(
156156
version_dir: &AbsolutePath,
157157
registry: Option<&str>,
158158
) -> Result<(), Error> {
159-
let vp_binary = version_dir.join("bin").join(if cfg!(windows) { "vp.exe" } else { "vp" });
159+
let vp_binary = version_dir.join("bin").join(crate::VP_BINARY_NAME);
160160

161161
if !tokio::fs::try_exists(&vp_binary).await.unwrap_or(false) {
162162
return Err(Error::Setup(
@@ -289,8 +289,7 @@ pub async fn swap_current_link(install_dir: &AbsolutePath, version: &str) -> Res
289289

290290
/// Refresh shims by running `vp env setup --refresh` with the new binary.
291291
pub async fn refresh_shims(install_dir: &AbsolutePath) -> Result<(), Error> {
292-
let vp_binary =
293-
install_dir.join("current").join("bin").join(if cfg!(windows) { "vp.exe" } else { "vp" });
292+
let vp_binary = install_dir.join("current").join("bin").join(crate::VP_BINARY_NAME);
294293

295294
if !tokio::fs::try_exists(&vp_binary).await.unwrap_or(false) {
296295
tracing::warn!(
@@ -324,18 +323,26 @@ pub async fn refresh_shims(install_dir: &AbsolutePath) -> Result<(), Error> {
324323
/// Used when the Node.js manager is disabled — ensures env files exist
325324
/// even without a full shim refresh.
326325
pub async fn create_env_files(install_dir: &AbsolutePath) -> Result<(), Error> {
327-
let vp_binary =
328-
install_dir.join("current").join("bin").join(if cfg!(windows) { "vp.exe" } else { "vp" });
326+
let vp_binary = install_dir.join("current").join("bin").join(crate::VP_BINARY_NAME);
329327

330328
if !tokio::fs::try_exists(&vp_binary).await.unwrap_or(false) {
331329
return Ok(());
332330
}
333331

334-
tokio::process::Command::new(vp_binary.as_path())
332+
let output = tokio::process::Command::new(vp_binary.as_path())
335333
.args(["env", "setup", "--env-only"])
336334
.output()
337335
.await?;
338336

337+
if !output.status.success() {
338+
let stderr = String::from_utf8_lossy(&output.stderr);
339+
tracing::warn!(
340+
"env setup --env-only exited with code {}, continuing anyway\n{}",
341+
output.status.code().unwrap_or(-1),
342+
stderr.trim()
343+
);
344+
}
345+
339346
Ok(())
340347
}
341348

crates/vite_setup/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ pub mod registry;
1515

1616
/// Maximum number of old versions to keep.
1717
pub const MAX_VERSIONS_KEEP: usize = 5;
18+
19+
/// Platform-specific binary name for the `vp` CLI.
20+
pub const VP_BINARY_NAME: &str = if cfg!(windows) { "vp.exe" } else { "vp" };

0 commit comments

Comments
 (0)