forked from electron-userland/electron-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackageManager.ts
More file actions
87 lines (72 loc) · 2.3 KB
/
packageManager.ts
File metadata and controls
87 lines (72 loc) · 2.3 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import * as path from "path"
import * as fs from "fs"
import * as which from "which"
export enum PM {
NPM = "npm",
YARN = "yarn",
PNPM = "pnpm",
YARN_BERRY = "yarn-berry",
}
// Cache for resolved paths
const pmPathCache: Record<PM, string | null | undefined> = {
[PM.NPM]: undefined,
[PM.YARN]: undefined,
[PM.PNPM]: undefined,
[PM.YARN_BERRY]: undefined,
}
function resolveCommand(pm: PM): string {
const fallback = pm === PM.YARN_BERRY ? "yarn" : pm
if (process.platform !== "win32") {
return fallback
}
try {
return which.sync(fallback)
} catch {
// If `which` fails (not found), still return the fallback string
return fallback
}
}
export function getPackageManagerCommand(pm: PM) {
if (pmPathCache[pm] !== undefined) {
return pmPathCache[pm]!
}
const resolved = resolveCommand(pm)
pmPathCache[pm] = resolved
return resolved
}
export function detectPackageManagerByEnv(pm: "npm" | "yarn" | "pnpm"): PM | null {
const ua = process.env.npm_config_user_agent ?? ""
const execPath = process.env.npm_execpath?.toLowerCase() ?? ""
const yarnVersion = process.env.YARN_VERSION
const isBerry = yarnVersion?.startsWith("2.") || yarnVersion?.startsWith("3.")
switch (pm) {
case "pnpm":
return ua.includes("pnpm") || execPath.includes("pnpm") || process.env.PNPM_HOME ? PM.PNPM : null
case "yarn":
if (ua.includes("yarn") || execPath.includes("yarn") || process.env.YARN_REGISTRY) {
return isBerry || ua.includes("yarn/2") || ua.includes("yarn/3") ? PM.YARN_BERRY : PM.YARN
}
return null
case "npm":
return ua.includes("npm") || execPath.includes("npm") || process.env.npm_package_json ? PM.NPM : null
default:
return null
}
}
export function detectPackageManagerByLockfile(cwd: string): PM | null {
const has = (file: string) => fs.existsSync(path.join(cwd, file))
const yarn = has("yarn.lock")
const pnpm = has("pnpm-lock.yaml")
const npm = has("package-lock.json")
const detected: PM[] = []
if (yarn) detected.push(PM.YARN)
if (pnpm) detected.push(PM.PNPM)
if (npm) detected.push(PM.NPM)
if (detected.length === 1) {
if (detected[0] === PM.YARN) {
return detectPackageManagerByEnv("yarn") === PM.YARN_BERRY ? PM.YARN_BERRY : PM.YARN
}
return detected[0]
}
return null
}