@@ -2,6 +2,10 @@
|
2 | 2 | import { pathToFileURL } from "node:url"; |
3 | 3 | |
4 | 4 | const allowedLifecyclePackageManagers = new Set(["pnpm", "npm", "yarn", "bun"]); |
| 5 | +const lifecyclePackageManagerLauncherAliases = new Map([ |
| 6 | +["yarnpkg", "yarn"], |
| 7 | +["yarn-berry", "yarn"], |
| 8 | +]); |
5 | 9 | |
6 | 10 | function normalizeEnvValue(value) { |
7 | 11 | return typeof value === "string" ? value.trim() : ""; |
@@ -15,6 +19,31 @@ function normalizeLifecyclePackageManagerName(value) {
|
15 | 19 | return allowedLifecyclePackageManagers.has(normalized) ? normalized : null; |
16 | 20 | } |
17 | 21 | |
| 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?js|mjs|cmd|ps1|exe)$/u, ""); |
| 26 | +const candidates = [launcherName, launcherName.replace(/-cli$/u, "")]; |
| 27 | + |
| 28 | +for (const candidate of candidates) { |
| 29 | +if (/^yarn(?:pkg)?-\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 | + |
18 | 47 | /** |
19 | 48 | * Detects the package manager running the current lifecycle script. |
20 | 49 | */ |
@@ -25,21 +54,7 @@ export function detectLifecyclePackageManager(env = process.env) {
|
25 | 54 | return normalizeLifecyclePackageManagerName(userAgentMatch[1]); |
26 | 55 | } |
27 | 56 | |
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); |
43 | 58 | } |
44 | 59 | |
45 | 60 | /** |
|