22import { pathToFileURL } from "node:url" ;
33
44const allowedLifecyclePackageManagers = new Set ( [ "pnpm" , "npm" , "yarn" , "bun" ] ) ;
5+ const lifecyclePackageManagerLauncherAliases = new Map ( [
6+ [ "yarnpkg" , "yarn" ] ,
7+ [ "yarn-berry" , "yarn" ] ,
8+ ] ) ;
59
610function normalizeEnvValue ( value ) {
711 return typeof value === "string" ? value . trim ( ) : "" ;
@@ -15,6 +19,31 @@ function normalizeLifecyclePackageManagerName(value) {
1519 return allowedLifecyclePackageManagers . has ( normalized ) ? normalized : null ;
1620}
1721
22+ function detectLifecyclePackageManagerFromExecPath ( value ) {
23+ const execPath = normalizeEnvValue ( value ) . toLowerCase ( ) ;
24+ const executableName = execPath . split ( / [ \\ / ] / u) . findLast ( ( segment ) => segment . length > 0 ) ?? "" ;
25+ const launcherName = executableName . replace ( / \. (?: c ? j s | m j s | c m d | p s 1 | e x e ) $ / u, "" ) ;
26+ const candidates = [ launcherName , launcherName . replace ( / - c l i $ / u, "" ) ] ;
27+
28+ for ( const candidate of candidates ) {
29+ if ( / ^ y a r n (?: p k g ) ? - \d / u. test ( candidate ) ) {
30+ return "yarn" ;
31+ }
32+
33+ const aliasedPackageManager = lifecyclePackageManagerLauncherAliases . get ( candidate ) ;
34+ if ( aliasedPackageManager ) {
35+ return aliasedPackageManager ;
36+ }
37+
38+ const packageManager = normalizeLifecyclePackageManagerName ( candidate ) ;
39+ if ( packageManager ) {
40+ return packageManager ;
41+ }
42+ }
43+
44+ return null ;
45+ }
46+
1847/**
1948 * Detects the package manager running the current lifecycle script.
2049 */
@@ -25,21 +54,7 @@ export function detectLifecyclePackageManager(env = process.env) {
2554 return normalizeLifecyclePackageManagerName ( userAgentMatch [ 1 ] ) ;
2655 }
2756
28- const execPath = normalizeEnvValue ( env . npm_execpath ) . toLowerCase ( ) ;
29- if ( execPath . includes ( "pnpm" ) ) {
30- return "pnpm" ;
31- }
32- if ( execPath . includes ( "npm" ) ) {
33- return "npm" ;
34- }
35- if ( execPath . includes ( "yarn" ) ) {
36- return "yarn" ;
37- }
38- if ( execPath . includes ( "bun" ) ) {
39- return "bun" ;
40- }
41-
42- return null ;
57+ return detectLifecyclePackageManagerFromExecPath ( env . npm_execpath ) ;
4358}
4459
4560/**
0 commit comments