Skip to content

Commit

Permalink
fix(cli): fix yarn berry detection (#11529)
Browse files Browse the repository at this point in the history
closes #11495
  • Loading branch information
amrbashir authored Nov 5, 2024
1 parent 4191a7a commit 100a445
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 46 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-yarn-berry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": "patch:bug"
"@tauri-apps/cli": "patch:bug"
---

Fix detecting yarn berry (v2 and higher) in various tauri cli commands.
5 changes: 1 addition & 4 deletions crates/tauri-cli/src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ pub fn run(options: Options) -> Result<()> {
})?;

if !metadata.rust_only {
if let Some(manager) = frontend_dir
.map(PackageManager::from_project)
.and_then(|managers| managers.into_iter().next())
{
if let Some(manager) = frontend_dir.map(PackageManager::from_project) {
let npm_version_req = version
.map(ToString::to_string)
.or(metadata.version_req.as_ref().map(|v| match manager {
Expand Down
37 changes: 35 additions & 2 deletions crates/tauri-cli/src/helpers/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ use anyhow::Context;
use crate::helpers::cross_command;
use std::{fmt::Display, path::Path, process::Command};

pub fn manager_version(package_manager: &str) -> Option<String> {
cross_command(package_manager)
.arg("-v")
.output()
.map(|o| {
if o.status.success() {
let v = String::from_utf8_lossy(o.stdout.as_slice()).to_string();
Some(v.split('\n').next().unwrap().to_string())
} else {
None
}
})
.ok()
.unwrap_or_default()
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum PackageManager {
Npm,
Expand Down Expand Up @@ -35,7 +51,16 @@ impl Display for PackageManager {
}

impl PackageManager {
pub fn from_project<P: AsRef<Path>>(path: P) -> Vec<Self> {
/// Detects package manager from the given directory, falls back to [`PackageManager::Npm`].
pub fn from_project<P: AsRef<Path>>(path: P) -> Self {
Self::all_from_project(path)
.first()
.copied()
.unwrap_or(Self::Npm)
}

/// Detects all possible package managers from the given directory.
pub fn all_from_project<P: AsRef<Path>>(path: P) -> Vec<Self> {
let mut found = Vec::new();

if let Ok(entries) = std::fs::read_dir(path) {
Expand All @@ -47,7 +72,15 @@ impl PackageManager {
} else if name.as_ref() == "pnpm-lock.yaml" {
found.push(PackageManager::Pnpm);
} else if name.as_ref() == "yarn.lock" {
found.push(PackageManager::Yarn);
let yarn = if manager_version("yarn")
.map(|v| v.chars().next().map(|c| c > '1').unwrap_or_default())
.unwrap_or(false)
{
PackageManager::YarnBerry
} else {
PackageManager::Yarn
};
found.push(yarn);
} else if name.as_ref() == "bun.lockb" {
found.push(PackageManager::Bun);
} else if name.as_ref() == "deno.lock" {
Expand Down
18 changes: 1 addition & 17 deletions crates/tauri-cli/src/info/env_nodejs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,7 @@
use super::{ActionResult, SectionItem, VersionMetadata};
use colored::Colorize;

use crate::helpers::cross_command;

pub fn manager_version(package_manager: &str) -> Option<String> {
cross_command(package_manager)
.arg("-v")
.output()
.map(|o| {
if o.status.success() {
let v = String::from_utf8_lossy(o.stdout.as_slice()).to_string();
Some(v.split('\n').next().unwrap().to_string())
} else {
None
}
})
.ok()
.unwrap_or_default()
}
use crate::helpers::{cross_command, npm::manager_version};

pub fn items(metadata: &VersionMetadata) -> Vec<SectionItem> {
let node_target_ver = metadata.js_cli.node.replace(">= ", "");
Expand Down
14 changes: 3 additions & 11 deletions crates/tauri-cli/src/info/packages_nodejs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT

use super::SectionItem;
use super::{env_nodejs::manager_version, VersionMetadata};
use super::VersionMetadata;
use colored::Colorize;
use serde::Deserialize;
use std::path::PathBuf;
Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn npm_latest_version(pm: &PackageManager, name: &str) -> crate::Result<Opti
}

pub fn package_manager(frontend_dir: &PathBuf) -> PackageManager {
let found = PackageManager::from_project(frontend_dir);
let found = PackageManager::all_from_project(frontend_dir);

if found.is_empty() {
println!(
Expand All @@ -98,15 +98,7 @@ pub fn package_manager(frontend_dir: &PathBuf) -> PackageManager {
);
}

if pkg_manager == PackageManager::Yarn
&& manager_version("yarn")
.map(|v| v.chars().next().map(|c| c > '1').unwrap_or_default())
.unwrap_or(false)
{
PackageManager::YarnBerry
} else {
pkg_manager
}
pkg_manager
}

pub fn items(
Expand Down
5 changes: 1 addition & 4 deletions crates/tauri-cli/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ impl Options {
)
})?;

let detected_package_manager = match PackageManager::from_project(&self.directory).first() {
Some(&package_manager) => package_manager,
None => PackageManager::Npm,
};
let detected_package_manager = PackageManager::from_project(&self.directory);

self.before_dev_command = self
.before_dev_command
Expand Down
5 changes: 1 addition & 4 deletions crates/tauri-cli/src/migrate/migrations/v1/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ pub fn migrate(frontend_dir: &Path) -> Result<Vec<String>> {
)
};

let pm = PackageManager::from_project(frontend_dir)
.into_iter()
.next()
.unwrap_or(PackageManager::Npm);
let pm = PackageManager::from_project(frontend_dir);

for pkg in ["@tauri-apps/cli", "@tauri-apps/api"] {
let version = pm
Expand Down
5 changes: 1 addition & 4 deletions crates/tauri-cli/src/migrate/migrations/v2_rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ pub fn run() -> Result<()> {
}

fn migrate_npm_dependencies(frontend_dir: &Path) -> Result<()> {
let pm = PackageManager::from_project(frontend_dir)
.into_iter()
.next()
.unwrap_or(PackageManager::Npm);
let pm = PackageManager::from_project(frontend_dir);

let mut install_deps = Vec::new();
for pkg in [
Expand Down

0 comments on commit 100a445

Please sign in to comment.