Skip to content

Commit 71666e9

Browse files
authored
fix(cli.rs): fix panic & use cmd to run yarn&npm on windows (#1511)
1 parent e447b8e commit 71666e9

File tree

2 files changed

+73
-9
lines changed

2 files changed

+73
-9
lines changed

.changes/clr.rs-fix-tauri-info.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"cli.rs": patch
3+
---
4+
5+
Fix `tauri info`
6+
* Properly detect `yarn` and `npm` versions on windows.
7+
* Fix a panic caused by a wrong field name in `metadata.json`

tooling/cli.rs/src/info.rs

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct JsCliVersionMetadata {
4141
#[derive(Deserialize)]
4242
#[serde(rename_all = "camelCase")]
4343
struct VersionMetadata {
44+
#[serde(rename = "cli.js")]
4445
js_cli: JsCliVersionMetadata,
4546
}
4647

@@ -86,7 +87,19 @@ fn crate_latest_version(name: &str) -> Option<String> {
8687

8788
fn npm_latest_version(use_yarn: bool, name: &str) -> crate::Result<Option<String>> {
8889
if use_yarn {
89-
let output = Command::new("yarn")
90+
let mut cmd;
91+
#[cfg(target_os = "windows")]
92+
{
93+
cmd = Command::new("cmd");
94+
cmd.arg("/c").arg("yarn");
95+
}
96+
97+
#[cfg(not(target_os = "windows"))]
98+
{
99+
cmd = Command::new("yarn")
100+
}
101+
102+
let output = cmd
90103
.arg("info")
91104
.arg(name)
92105
.args(&["version", "--json"])
@@ -99,11 +112,19 @@ fn npm_latest_version(use_yarn: bool, name: &str) -> crate::Result<Option<String
99112
Ok(None)
100113
}
101114
} else {
102-
let output = Command::new("npm")
103-
.arg("show")
104-
.arg(name)
105-
.arg("version")
106-
.output()?;
115+
let mut cmd;
116+
#[cfg(target_os = "windows")]
117+
{
118+
cmd = Command::new("cmd");
119+
cmd.arg("/c").arg("npm");
120+
}
121+
122+
#[cfg(not(target_os = "windows"))]
123+
{
124+
cmd = Command::new("npm")
125+
}
126+
127+
let output = cmd.arg("show").arg(name).arg("version").output()?;
107128
if output.status.success() {
108129
let stdout = String::from_utf8_lossy(&output.stdout);
109130
Ok(Some(stdout.replace("\n", "")))
@@ -119,14 +140,38 @@ fn npm_package_version<P: AsRef<Path>>(
119140
app_dir: P,
120141
) -> crate::Result<Option<String>> {
121142
let output = if use_yarn {
122-
Command::new("yarn")
143+
let mut cmd;
144+
#[cfg(target_os = "windows")]
145+
{
146+
cmd = Command::new("cmd");
147+
cmd.arg("/c").arg("yarn");
148+
}
149+
150+
#[cfg(not(target_os = "windows"))]
151+
{
152+
cmd = Command::new("yarn")
153+
}
154+
155+
cmd
123156
.args(&["list", "--pattern"])
124157
.arg(name)
125158
.args(&["--depth", "0"])
126159
.current_dir(app_dir)
127160
.output()?
128161
} else {
129-
Command::new("npm")
162+
let mut cmd;
163+
#[cfg(target_os = "windows")]
164+
{
165+
cmd = Command::new("cmd");
166+
cmd.arg("/c").arg("npm");
167+
}
168+
169+
#[cfg(not(target_os = "windows"))]
170+
{
171+
cmd = Command::new("npm")
172+
}
173+
174+
cmd
130175
.arg("list")
131176
.arg(name)
132177
.args(&["version", "--depth", "0"])
@@ -148,7 +193,19 @@ fn npm_package_version<P: AsRef<Path>>(
148193
}
149194

150195
fn get_version(command: &str, args: &[&str]) -> crate::Result<Option<String>> {
151-
let output = Command::new(command).args(args).arg("--version").output()?;
196+
let mut cmd;
197+
#[cfg(target_os = "windows")]
198+
{
199+
cmd = Command::new("cmd");
200+
cmd.arg("/c").arg(command);
201+
}
202+
203+
#[cfg(not(target_os = "windows"))]
204+
{
205+
cmd = Command::new(command)
206+
}
207+
208+
let output = cmd.args(args).arg("--version").output()?;
152209
let version = if output.status.success() {
153210
Some(String::from_utf8_lossy(&output.stdout).replace("\n", ""))
154211
} else {

0 commit comments

Comments
 (0)