@@ -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 ) ]
2738pub 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 _ => ( ) ,
0 commit comments