@@ -7,6 +7,22 @@ use anyhow::Context;
77use crate :: helpers:: cross_command;
88use std:: { fmt:: Display , path:: Path , process:: Command } ;
99
10+ pub fn manager_version ( package_manager : & str ) -> Option < String > {
11+ cross_command ( package_manager)
12+ . arg ( "-v" )
13+ . output ( )
14+ . map ( |o| {
15+ if o. status . success ( ) {
16+ let v = String :: from_utf8_lossy ( o. stdout . as_slice ( ) ) . to_string ( ) ;
17+ Some ( v. split ( '\n' ) . next ( ) . unwrap ( ) . to_string ( ) )
18+ } else {
19+ None
20+ }
21+ } )
22+ . ok ( )
23+ . unwrap_or_default ( )
24+ }
25+
1026#[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
1127pub enum PackageManager {
1228 Npm ,
@@ -35,7 +51,16 @@ impl Display for PackageManager {
3551}
3652
3753impl PackageManager {
38- pub fn from_project < P : AsRef < Path > > ( path : P ) -> Vec < Self > {
54+ /// Detects package manager from the given directory, falls back to [`PackageManager::Npm`].
55+ pub fn from_project < P : AsRef < Path > > ( path : P ) -> Self {
56+ Self :: all_from_project ( path)
57+ . first ( )
58+ . copied ( )
59+ . unwrap_or ( Self :: Npm )
60+ }
61+
62+ /// Detects all possible package managers from the given directory.
63+ pub fn all_from_project < P : AsRef < Path > > ( path : P ) -> Vec < Self > {
3964 let mut found = Vec :: new ( ) ;
4065
4166 if let Ok ( entries) = std:: fs:: read_dir ( path) {
@@ -47,7 +72,15 @@ impl PackageManager {
4772 } else if name. as_ref ( ) == "pnpm-lock.yaml" {
4873 found. push ( PackageManager :: Pnpm ) ;
4974 } else if name. as_ref ( ) == "yarn.lock" {
50- found. push ( PackageManager :: Yarn ) ;
75+ let yarn = if manager_version ( "yarn" )
76+ . map ( |v| v. chars ( ) . next ( ) . map ( |c| c > '1' ) . unwrap_or_default ( ) )
77+ . unwrap_or ( false )
78+ {
79+ PackageManager :: YarnBerry
80+ } else {
81+ PackageManager :: Yarn
82+ } ;
83+ found. push ( yarn) ;
5184 } else if name. as_ref ( ) == "bun.lockb" {
5285 found. push ( PackageManager :: Bun ) ;
5386 } else if name. as_ref ( ) == "deno.lock" {
0 commit comments