1import { execSync } from 'child_process'; 2 3export type PackageManagerName = 'npm' | 'pnpm' | 'yarn'; 4 5/** Determine which package manager to use for installing dependencies based on how the process was started. */ 6export function resolvePackageManager(): PackageManagerName { 7 // Attempt to detect if the user started the command using `yarn` or `pnpm` 8 const userAgent = process.env.npm_config_user_agent; 9 10 if (userAgent?.startsWith('yarn')) { 11 return 'yarn'; 12 } else if (userAgent?.startsWith('pnpm')) { 13 return 'pnpm'; 14 } else if (userAgent?.startsWith('npm')) { 15 return 'npm'; 16 } 17 18 // Try availability 19 if (isPackageManagerAvailable('yarn')) { 20 return 'yarn'; 21 } else if (isPackageManagerAvailable('pnpm')) { 22 return 'pnpm'; 23 } 24 25 return 'npm'; 26} 27 28function isPackageManagerAvailable(manager: PackageManagerName): boolean { 29 try { 30 execSync(`${manager} --version`, { stdio: 'ignore' }); 31 return true; 32 } catch {} 33 return false; 34} 35