Skip to content

Commit

Permalink
fix(cli.rs): fix panic & use cmd to run yarn&npm on windows (#1511
Browse files Browse the repository at this point in the history
)
  • Loading branch information
amrbashir authored Apr 16, 2021
1 parent e447b8e commit 71666e9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .changes/clr.rs-fix-tauri-info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"cli.rs": patch
---

Fix `tauri info`
* Properly detect `yarn` and `npm` versions on windows.
* Fix a panic caused by a wrong field name in `metadata.json`
75 changes: 66 additions & 9 deletions tooling/cli.rs/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct JsCliVersionMetadata {
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct VersionMetadata {
#[serde(rename = "cli.js")]
js_cli: JsCliVersionMetadata,
}

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

fn npm_latest_version(use_yarn: bool, name: &str) -> crate::Result<Option<String>> {
if use_yarn {
let output = Command::new("yarn")
let mut cmd;
#[cfg(target_os = "windows")]
{
cmd = Command::new("cmd");
cmd.arg("/c").arg("yarn");
}

#[cfg(not(target_os = "windows"))]
{
cmd = Command::new("yarn")
}

let output = cmd
.arg("info")
.arg(name)
.args(&["version", "--json"])
Expand All @@ -99,11 +112,19 @@ fn npm_latest_version(use_yarn: bool, name: &str) -> crate::Result<Option<String
Ok(None)
}
} else {
let output = Command::new("npm")
.arg("show")
.arg(name)
.arg("version")
.output()?;
let mut cmd;
#[cfg(target_os = "windows")]
{
cmd = Command::new("cmd");
cmd.arg("/c").arg("npm");
}

#[cfg(not(target_os = "windows"))]
{
cmd = Command::new("npm")
}

let output = cmd.arg("show").arg(name).arg("version").output()?;
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
Ok(Some(stdout.replace("\n", "")))
Expand All @@ -119,14 +140,38 @@ fn npm_package_version<P: AsRef<Path>>(
app_dir: P,
) -> crate::Result<Option<String>> {
let output = if use_yarn {
Command::new("yarn")
let mut cmd;
#[cfg(target_os = "windows")]
{
cmd = Command::new("cmd");
cmd.arg("/c").arg("yarn");
}

#[cfg(not(target_os = "windows"))]
{
cmd = Command::new("yarn")
}

cmd
.args(&["list", "--pattern"])
.arg(name)
.args(&["--depth", "0"])
.current_dir(app_dir)
.output()?
} else {
Command::new("npm")
let mut cmd;
#[cfg(target_os = "windows")]
{
cmd = Command::new("cmd");
cmd.arg("/c").arg("npm");
}

#[cfg(not(target_os = "windows"))]
{
cmd = Command::new("npm")
}

cmd
.arg("list")
.arg(name)
.args(&["version", "--depth", "0"])
Expand All @@ -148,7 +193,19 @@ fn npm_package_version<P: AsRef<Path>>(
}

fn get_version(command: &str, args: &[&str]) -> crate::Result<Option<String>> {
let output = Command::new(command).args(args).arg("--version").output()?;
let mut cmd;
#[cfg(target_os = "windows")]
{
cmd = Command::new("cmd");
cmd.arg("/c").arg(command);
}

#[cfg(not(target_os = "windows"))]
{
cmd = Command::new(command)
}

let output = cmd.args(args).arg("--version").output()?;
let version = if output.status.success() {
Some(String::from_utf8_lossy(&output.stdout).replace("\n", ""))
} else {
Expand Down

0 comments on commit 71666e9

Please sign in to comment.