Skip to content

Commit 1d5f337

Browse files
nekomoyifengmk2
andauthored
feat(cli): show package manager and Node.js version in --version (#918)
Enhances `vp --version` to display configured package manager and node version. Example output: ``` VITE+ - The Unified Toolchain for the Web vp v0.0.0 Local vite-plus: vite-plus v0.0.0 Tools: vite v8.0.0 rolldown v1.0.0-rc.9 vitest v4.1.0 oxfmt v0.40.0 oxlint v1.55.0 oxlint-tsgolint v0.16.0 tsdown v0.21.2 Environment: Package manager pnpm v10.28.0 Node.js v22.18.0 (.node-version) ``` Closes #889 --------- Co-authored-by: MK (fengmk2) <fengmk2@gmail.com>
1 parent f9c5cfb commit 1d5f337

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

crates/vite_global_cli/src/commands/version.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use std::{
99

1010
use owo_colors::OwoColorize;
1111
use serde::Deserialize;
12+
use vite_install::get_package_manager_type_and_version;
13+
use vite_js_runtime::{VersionSource, resolve_node_version};
1214
use vite_path::AbsolutePathBuf;
15+
use vite_workspace::find_workspace_root;
1316

1417
use crate::{error::Error, help};
1518

@@ -64,6 +67,8 @@ const TOOL_SPECS: [ToolSpec; 7] = [
6467
},
6568
];
6669

70+
const NOT_FOUND: &str = "Not found";
71+
6772
fn read_package_json(package_json_path: &Path) -> Option<PackageJson> {
6873
let content = fs::read_to_string(package_json_path).ok()?;
6974
serde_json::from_str(&content).ok()
@@ -123,10 +128,21 @@ fn print_rows(title: &str, rows: &[(&str, String)]) {
123128
fn format_version(version: Option<String>) -> String {
124129
match version {
125130
Some(v) => format!("v{v}"),
126-
None => "Not found".to_string(),
131+
None => NOT_FOUND.to_string(),
127132
}
128133
}
129134

135+
async fn get_node_version_info(cwd: &AbsolutePathBuf) -> Option<(String, String)> {
136+
let resolution_opt = resolve_node_version(cwd, true).await.ok()?;
137+
let resolution = resolution_opt?;
138+
let source_label = match resolution.source {
139+
VersionSource::NodeVersionFile => ".node-version",
140+
VersionSource::EnginesNode => "engines.node",
141+
VersionSource::DevEnginesRuntime => "devEngines.runtime",
142+
};
143+
Some((resolution.version.to_string(), source_label.to_string()))
144+
}
145+
130146
/// Execute the `--version` command.
131147
pub async fn execute(cwd: AbsolutePathBuf) -> Result<ExitStatus, Error> {
132148
println!("{}", vite_shared::header::vite_plus_header());
@@ -135,6 +151,7 @@ pub async fn execute(cwd: AbsolutePathBuf) -> Result<ExitStatus, Error> {
135151
println!("vp v{}", env!("CARGO_PKG_VERSION"));
136152
println!();
137153

154+
// Local vite-plus and tools
138155
let local = find_local_vite_plus(cwd.as_path());
139156
print_rows(
140157
"Local vite-plus",
@@ -151,6 +168,26 @@ pub async fn execute(cwd: AbsolutePathBuf) -> Result<ExitStatus, Error> {
151168
})
152169
.collect::<Vec<_>>();
153170
print_rows("Tools", &tool_rows);
171+
println!();
172+
173+
// Environment info
174+
let package_manager_info = find_workspace_root(&cwd)
175+
.ok()
176+
.and_then(|(root, _)| {
177+
get_package_manager_type_and_version(&root, None)
178+
.ok()
179+
.map(|(pm, v, _)| format!("{pm} v{v}"))
180+
})
181+
.unwrap_or(NOT_FOUND.to_string());
182+
183+
let node_info = get_node_version_info(&cwd)
184+
.await
185+
.map(|(v, s)| format!("v{v} ({s})"))
186+
.unwrap_or(NOT_FOUND.to_string());
187+
188+
let env_rows = [("Package manager", package_manager_info), ("Node.js", node_info)];
189+
190+
print_rows("Environment", &env_rows);
154191

155192
Ok(ExitStatus::default())
156193
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ Tools:
6969
oxlint-tsgolint v<semver>
7070
tsdown v<semver>
7171

72+
Environment:
73+
Package manager Not found
74+
Node.js Not found
75+
7276
> vp install -h # show install help message
7377
VITE+ - The Unified Toolchain for the Web
7478

packages/cli/snap-tests-global/command-version-no-side-effects/snap.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ Tools:
1515
oxlint-tsgolint v<semver>
1616
tsdown v<semver>
1717

18+
Environment:
19+
Package manager Not found
20+
Node.js Not found
21+
1822
> test -f .node-version && echo 'FAIL: .node-version was created' || echo 'OK: no .node-version created'
1923
OK: no .node-version created
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "command-version-with-env",
3+
"version": "1.0.0",
4+
"private": true,
5+
"engines": {
6+
"node": "22.18.0"
7+
},
8+
"packageManager": "pnpm@10.20.0"
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
> vp --version
2+
VITE+ - The Unified Toolchain for the Web
3+
4+
vp v<semver>
5+
6+
Local vite-plus:
7+
vite-plus v<semver>
8+
9+
Tools:
10+
vite v<semver>
11+
rolldown v<semver>
12+
vitest v<semver>
13+
oxfmt v<semver>
14+
oxlint v<semver>
15+
oxlint-tsgolint v<semver>
16+
tsdown v<semver>
17+
18+
Environment:
19+
Package manager pnpm v<semver>
20+
Node.js v<semver> (engines.node)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"ignoredPlatforms": ["win32"],
3+
"commands": ["vp --version"]
4+
}

0 commit comments

Comments
 (0)