diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_pin_nvmrc/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_pin_nvmrc/snapshots.toml index cf04e5170f..9a2a6669a7 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_pin_nvmrc/snapshots.toml +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_pin_nvmrc/snapshots.toml @@ -39,6 +39,16 @@ steps = [ ["vpt", "stat-file", ".nvmrc", "--assert", "file"], ] +[[case]] +name = "node_version_pin_reports_created_then_updated" +vp = "global" +seed-runtime = false +steps = [ + { argv = ["vp", "env", "pin", "22.13.0", "--target", "node-version", "--no-install"], comment = "a new .node-version is reported as created" }, + { argv = ["vp", "env", "pin", "24.11.0", "--no-install", "--force"], comment = "overwriting the existing .node-version is reported as updated" }, + ["vpt", "print-file", ".node-version"], +] + [[case]] name = "dev_engines_keeps_priority_over_nvmrc" vp = "global" diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_pin_nvmrc/snapshots/explicit_nvmrc_target_can_create_a_pin.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_pin_nvmrc/snapshots/explicit_nvmrc_target_can_create_a_pin.md index e80442e0bb..529a8f2c02 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_pin_nvmrc/snapshots/explicit_nvmrc_target_can_create_a_pin.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_pin_nvmrc/snapshots/explicit_nvmrc_target_can_create_a_pin.md @@ -9,7 +9,7 @@ VITE+ - The Unified Toolchain for the Web ✓ Pinned Node.js version to 22.13.0 - Updated .nvmrc in + Created .nvmrc in note: Version will be downloaded on first use. ``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_pin_nvmrc/snapshots/node_version_keeps_priority_over_nvmrc.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_pin_nvmrc/snapshots/node_version_keeps_priority_over_nvmrc.md index 57ce3ed89b..50794428b8 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_pin_nvmrc/snapshots/node_version_keeps_priority_over_nvmrc.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_pin_nvmrc/snapshots/node_version_keeps_priority_over_nvmrc.md @@ -10,7 +10,7 @@ VITE+ - The Unified Toolchain for the Web ✓ Pinned Node.js version to 22.13.0 - Created .node-version in + Updated .node-version in note: Version will be downloaded on first use. ``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_pin_nvmrc/snapshots/node_version_pin_reports_created_then_updated.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_pin_nvmrc/snapshots/node_version_pin_reports_created_then_updated.md new file mode 100644 index 0000000000..b1c75e608f --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_pin_nvmrc/snapshots/node_version_pin_reports_created_then_updated.md @@ -0,0 +1,31 @@ +# node_version_pin_reports_created_then_updated + +## `vp env pin 22.13.0 --target node-version --no-install` + +a new .node-version is reported as created + +``` +VITE+ - The Unified Toolchain for the Web + +✓ Pinned Node.js version to 22.13.0 + Created .node-version in +note: Version will be downloaded on first use. +``` + +## `vp env pin 24.11.0 --no-install --force` + +overwriting the existing .node-version is reported as updated + +``` +VITE+ - The Unified Toolchain for the Web + +✓ Pinned Node.js version to 24.11.0 + Updated .node-version in +note: Version will be downloaded on first use. +``` + +## `vpt print-file .node-version` + +``` +24.11.0 +``` diff --git a/crates/vp_global_cli/src/commands/env/pin.rs b/crates/vp_global_cli/src/commands/env/pin.rs index d98abfeca0..2ef60b7fb9 100644 --- a/crates/vp_global_cli/src/commands/env/pin.rs +++ b/crates/vp_global_cli/src/commands/env/pin.rs @@ -379,6 +379,15 @@ fn print_pin_success(input_version: &str, resolved_version: &str, was_alias: boo } } +/// Print which pin file was written, distinguishing a new file from an overwrite. +fn print_pin_file_written(file_name: &str, existed: bool, cwd: &AbsolutePathBuf) { + let action = if existed { "Updated" } else { "Created" }; + vp_shared::output::print_stdout_line(format_args!( + " {action} {file_name} in {}", + cwd.as_path().display() + )); +} + /// Pin by writing the `.node-version` file. /// /// Returns `true` when the pin was written, `false` when nothing changed @@ -393,7 +402,8 @@ async fn pin_node_version_file( let node_version_path = cwd.join(NODE_VERSION_FILE); // Check if .node-version already exists - if tokio::fs::try_exists(&node_version_path).await.unwrap_or(false) { + let exists = tokio::fs::try_exists(&node_version_path).await.unwrap_or(false); + if exists { let existing_content = tokio::fs::read_to_string(&node_version_path).await?; let existing_version = existing_content.trim(); if !confirm_overwrite_pin( @@ -410,11 +420,7 @@ async fn pin_node_version_file( tokio::fs::write(&node_version_path, format!("{resolved_version}\n")).await?; print_pin_success(input_version, resolved_version, was_alias); - vp_shared::output::print_stdout_line(format_args!( - " Created {} in {}", - NODE_VERSION_FILE, - cwd.as_path().display() - )); + print_pin_file_written(NODE_VERSION_FILE, exists, cwd); // If a devEngines.runtime range is declared and no longer satisfied, offer to // sync it in interactive terminals and warn otherwise (rfcs/dev-engines.md) @@ -442,9 +448,9 @@ async fn pin_nvmrc_file( force: bool, ) -> Result { let path = cwd.join(NVMRC_FILE); - let mut content = match tokio::fs::read_to_string(&path).await { - Ok(content) => content, - Err(error) if error.kind() == std::io::ErrorKind::NotFound => String::new(), + let (mut content, exists) = match tokio::fs::read_to_string(&path).await { + Ok(content) => (content, true), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => (String::new(), false), Err(error) => return Err(error.into()), }; if let Some(span) = nvmrc_version_span(&content)? { @@ -468,10 +474,7 @@ async fn pin_nvmrc_file( } tokio::fs::write(&path, content).await?; print_pin_success(input_version, resolved_version, was_alias); - vp_shared::output::print_stdout_line(format_args!( - " Updated {NVMRC_FILE} in {}", - cwd.as_path().display() - )); + print_pin_file_written(NVMRC_FILE, exists, cwd); Ok(true) } diff --git a/rfcs/env-command.md b/rfcs/env-command.md index 8fdf2d5695..23d17cf15b 100644 --- a/rfcs/env-command.md +++ b/rfcs/env-command.md @@ -1481,6 +1481,7 @@ $ vp env pin 22.13.0 .node-version already exists with version 20.18.0 Overwrite with 22.13.0? (y/n): y ✓ Pinned Node.js version to 22.13.0 + Updated .node-version in /Users/user/projects/my-app ``` Use `--force` to skip confirmation: @@ -1488,6 +1489,7 @@ Use `--force` to skip confirmation: ```bash $ vp env pin 22.13.0 --force ✓ Pinned Node.js version to 22.13.0 + Updated .node-version in /Users/user/projects/my-app ``` When the target is already pinned to the same version, the command no-ops (with or without `--force`):