-
Notifications
You must be signed in to change notification settings - Fork 27.4k
/
Copy pathget-pkg-manager.ts
36 lines (34 loc) · 1012 Bytes
/
get-pkg-manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import fs from 'fs'
import path from 'path'
import { execSync } from 'child_process'
export type PackageManager = 'npm' | 'pnpm' | 'yarn'
export function getPkgManager(baseDir: string): PackageManager {
try {
for (const { lockFile, packageManager } of [
{ lockFile: 'yarn.lock', packageManager: 'yarn' },
{ lockFile: 'pnpm-lock.yaml', packageManager: 'pnpm' },
{ lockFile: 'package-lock.json', packageManager: 'npm' },
]) {
if (fs.existsSync(path.join(baseDir, lockFile))) {
return packageManager as PackageManager
}
}
const userAgent = process.env.npm_config_user_agent
if (userAgent) {
if (userAgent.startsWith('yarn')) {
return 'yarn'
} else if (userAgent.startsWith('pnpm')) {
return 'pnpm'
}
}
try {
execSync('yarn --version', { stdio: 'ignore' })
return 'yarn'
} catch {
execSync('pnpm --version', { stdio: 'ignore' })
return 'pnpm'
}
} catch {
return 'npm'
}
}