Skip to content

Commit ad3fd38

Browse files
feat(cli): try detect package manager from env (#13152)
* fix(cli): try detect package manager from env * Typo
1 parent 3752fed commit ad3fd38

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:enhance
3+
"@tauri-apps/cli": patch:enhance
4+
---
5+
6+
Detect package manager from environment variable `npm_config_user_agent` first

crates/tauri-cli/src/helpers/npm.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ pub fn manager_version(package_manager: &str) -> Option<String> {
2323
.unwrap_or_default()
2424
}
2525

26+
fn detect_yarn_or_berry() -> PackageManager {
27+
if manager_version("yarn")
28+
.map(|v| v.chars().next().map(|c| c > '1').unwrap_or_default())
29+
.unwrap_or(false)
30+
{
31+
PackageManager::YarnBerry
32+
} else {
33+
PackageManager::Yarn
34+
}
35+
}
36+
2637
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
2738
pub enum PackageManager {
2839
Npm,
@@ -59,8 +70,25 @@ impl PackageManager {
5970
.unwrap_or(Self::Npm)
6071
}
6172

73+
/// Detects package manager from the `npm_config_user_agent` environment variable
74+
fn from_environment_variable() -> Option<Self> {
75+
let npm_config_user_agent = std::env::var("npm_config_user_agent").ok()?;
76+
match npm_config_user_agent {
77+
user_agent if user_agent.starts_with("pnpm/") => Some(Self::Pnpm),
78+
user_agent if user_agent.starts_with("deno/") => Some(Self::Deno),
79+
user_agent if user_agent.starts_with("bun/") => Some(Self::Bun),
80+
user_agent if user_agent.starts_with("yarn/") => Some(detect_yarn_or_berry()),
81+
user_agent if user_agent.starts_with("npm/") => Some(Self::Npm),
82+
_ => None,
83+
}
84+
}
85+
6286
/// Detects all possible package managers from the given directory.
6387
pub fn all_from_project<P: AsRef<Path>>(path: P) -> Vec<Self> {
88+
if let Some(from_env) = Self::from_environment_variable() {
89+
return vec![from_env];
90+
}
91+
6492
let mut found = Vec::new();
6593

6694
if let Ok(entries) = std::fs::read_dir(path) {
@@ -70,17 +98,7 @@ impl PackageManager {
7098
match name.as_ref() {
7199
"package-lock.json" => found.push(PackageManager::Npm),
72100
"pnpm-lock.yaml" => found.push(PackageManager::Pnpm),
73-
"yarn.lock" => {
74-
let yarn = if manager_version("yarn")
75-
.map(|v| v.chars().next().map(|c| c > '1').unwrap_or_default())
76-
.unwrap_or(false)
77-
{
78-
PackageManager::YarnBerry
79-
} else {
80-
PackageManager::Yarn
81-
};
82-
found.push(yarn);
83-
}
101+
"yarn.lock" => found.push(detect_yarn_or_berry()),
84102
"bun.lock" | "bun.lockb" => found.push(PackageManager::Bun),
85103
"deno.lock" => found.push(PackageManager::Deno),
86104
_ => (),

crates/tauri-macros/src/command/handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl Parse for Handler {
6666
}
6767

6868
/// Try to get the plugin name by parsing the input for a `#![plugin(...)]` attribute,
69-
/// if it's not present, try getting it from `CARGO_PKG_NAME` enviroment variable
69+
/// if it's not present, try getting it from `CARGO_PKG_NAME` environment variable
7070
fn try_get_plugin_name(input: &ParseBuffer<'_>) -> Result<Option<String>, syn::Error> {
7171
if let Ok(attrs) = input.call(Attribute::parse_inner) {
7272
for attr in attrs {

0 commit comments

Comments
 (0)