-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathdetectPackageManager.ts
123 lines (113 loc) Β· 3.45 KB
/
detectPackageManager.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import fs from "fs-extra"
import { join } from "./path"
import chalk from "chalk"
import process from "process"
import findWorkspaceRoot from "find-yarn-workspace-root"
export type PackageManager = "yarn" | "npm" | "npm-shrinkwrap" | "bun"
function printNoYarnLockfileError() {
console.log(`
${chalk.red.bold("**ERROR**")} ${chalk.red(
`The --use-yarn option was specified but there is no yarn.lock file`,
)}
`)
}
function printNoBunLockfileError() {
console.log(`
${chalk.red.bold("**ERROR**")} ${chalk.red(
`The --use-bun option was specified but there is no bun.lockb file`,
)}
`)
}
function printNoLockfilesError() {
console.log(`
${chalk.red.bold("**ERROR**")} ${chalk.red(
`No package-lock.json, npm-shrinkwrap.json, yarn.lock, or bun.lockb file.
You must use either npm@>=5, yarn, npm-shrinkwrap, or bun to manage this project's
dependencies.`,
)}
`)
}
function printSelectingDefaultMessage() {
console.info(
`${chalk.bold(
"patch-package",
)}: you have multiple lockfiles, e.g. yarn.lock and package-lock.json
Defaulting to using ${chalk.bold("npm")}
You can override this setting by passing --use-yarn, --use-bun, or
deleting the conflicting lockfile if you don't need it
`,
)
}
function printSelectingDefaultYarnMessage() {
console.info(
`${chalk.bold(
"patch-package",
)}: you have both yarn.lock and bun.lockb lockfiles
Defaulting to using ${chalk.bold("yarn")}
You can override this setting by passing --use-bun, or
deleting yarn.lock if you don't need it
`,
)
}
function checkForYarnOverride(overridePackageManager: PackageManager | null) {
if (overridePackageManager === "yarn") {
printNoYarnLockfileError()
process.exit(1)
}
}
function checkForBunOverride(overridePackageManager: PackageManager | null) {
if (overridePackageManager === "bun") {
printNoBunLockfileError()
process.exit(1)
}
}
export const detectPackageManager = (
appRootPath: string,
overridePackageManager: PackageManager | null,
): PackageManager => {
const packageLockExists = fs.existsSync(
join(appRootPath, "package-lock.json"),
)
const shrinkWrapExists = fs.existsSync(
join(appRootPath, "npm-shrinkwrap.json"),
)
const yarnLockExists = fs.existsSync(
join(findWorkspaceRoot() ?? appRootPath, "yarn.lock"),
)
// Bun workspaces seem to work the same as yarn workspaces - https://bun.sh/docs/install/workspaces
const bunLockbExists = fs.existsSync(
join(findWorkspaceRoot() ?? appRootPath, "bun.lockb"),
)
if (
[
packageLockExists || shrinkWrapExists,
yarnLockExists,
bunLockbExists,
].filter(Boolean).length > 1
) {
if (overridePackageManager) {
return overridePackageManager
}
if (!packageLockExists && !shrinkWrapExists) {
// The only case where we don't want to default to npm is when we have both yarn and bun lockfiles.
printSelectingDefaultYarnMessage()
return "yarn"
}
printSelectingDefaultMessage()
return shrinkWrapExists ? "npm-shrinkwrap" : "npm"
} else if (packageLockExists || shrinkWrapExists) {
checkForYarnOverride(overridePackageManager)
checkForBunOverride(overridePackageManager)
return shrinkWrapExists ? "npm-shrinkwrap" : "npm"
} else if (yarnLockExists) {
checkForBunOverride(overridePackageManager)
return "yarn"
} else if (bunLockbExists) {
checkForYarnOverride(overridePackageManager)
return "bun"
} else {
printNoLockfilesError()
process.exit(1)
}
throw Error()
}