forked from ds300/patch-package
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdetectPackageManager.ts
193 lines (179 loc) Β· 5.58 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import fs from "fs-extra"
import { join } from "./path"
//import path from "path"
import chalk from "chalk"
import process from "process"
//import findYarnWorkspaceRoot from "find-yarn-workspace-root"
export type PackageManager = "yarn" | "npm" | "npm-shrinkwrap" | "pnpm"
//const isVerbose = global.patchPackageIsVerbose
const isDebug = global.patchPackageIsDebug
/*
function printNoYarnLockfileError() {
console.error(`
${chalk.red.bold("**ERROR**")} ${chalk.red(
`The --use-yarn option was specified but there is no yarn.lock file`,
)}
`)
}
*/
function printNoLockfilesError() {
console.error(`
${chalk.red.bold("**ERROR**")} ${chalk.red(
`No package-lock.json, npm-shrinkwrap.json, or yarn.lock file.
You must use either npm@>=5, yarn, or npm-shrinkwrap to manage this project's
dependencies.`,
)}
`)
}
/*
function printSelectingDefaultMessage() {
console.info(
`${chalk.bold(
"patch-package",
)}: you have both yarn.lock and package-lock.json
Defaulting to using ${chalk.bold("npm")}
You can override this setting by passing --use-yarn or deleting
package-lock.json if you don't need it
`,
)
}
*/
/*
function isFileInPnpmRoot(rootPath: string, filename: string): boolean {
const osRoot = path.parse(rootPath).root
let currentDir = rootPath
while (currentDir !== osRoot) {
if (fs.existsSync(path.join(currentDir, "pnpm-workspace.yaml"))) {
// Found workspace root. If the sought file is in the workspace root,
// we're good.
if (fs.existsSync(path.join(currentDir, filename))) {
return true
} else {
return false
}
} else {
currentDir = path.resolve(currentDir, "..")
}
}
return false
}
*/
function pickManager(managersFound) {
if (managersFound.includes("yarn")) return "yarn"
if (managersFound.includes("pnpm")) return "pnpm"
if (managersFound.includes("npm")) return "npm"
return null
}
export const detectPackageManager = (
appRootPath: string,
overridePackageManager: PackageManager | null,
): PackageManager => {
if (isDebug) {
console.log(`patch-package/detectPackageManager:`)
console.dir({
appRootPath,
overridePackageManager,
})
}
const managerOfLockName = {
'package-lock.json': 'npm',
'npm-shrinkwrap.json': 'npm',
'yarn.lock': 'yarn',
'pnpm-lock.yaml': 'pnpm',
'shrinkwrap.yaml': 'pnpm',
}
const pathParts = appRootPath.split("/")
for (let depth = pathParts.length; depth > 0; depth--) {
const workspaceCandidate = pathParts.slice(0, depth).join("/")
if (isDebug) {
console.log(`detectPackageManager: workspaceCandidate: ${workspaceCandidate}`)
}
// TODO fast path
//if (overridePackageManager) {
// ...
//}
const lockfilesFound: string[] = (
([
// TODO async
['package-lock.json', fs.existsSync(join(workspaceCandidate, "package-lock.json"))],
['npm-shrinkwrap.json', fs.existsSync(join(workspaceCandidate, "npm-shrinkwrap.json"))], // rare
['yarn.lock', fs.existsSync(join(workspaceCandidate, "yarn.lock"))],
['pnpm-lock.yaml', fs.existsSync(join(workspaceCandidate, "pnpm-lock.yaml"))],
['shrinkwrap.yaml', fs.existsSync(join(workspaceCandidate, "shrinkwrap.yaml"))], // rare
] as Array<[string, boolean]>)
.filter(([_file, exists]) => exists)
.map(([file, _exists]) => file)
)
if (isDebug) {
console.log(`detectPackageManager: lockfilesFound: ${lockfilesFound.join(' ')}`)
}
if (lockfilesFound.length == 0) {
continue
}
if (lockfilesFound.length == 1) {
return managerOfLockName[lockfilesFound[0]]
}
// found multiple lockfiles
const managersFound = lockfilesFound.map(file => managerOfLockName[file])
if (overridePackageManager) {
// TODO better. if overridePackageManager is set, we can skip some fs.existsSync calls
if (managersFound.includes(overridePackageManager)) {
return overridePackageManager
}
continue
}
const manager = pickManager(managersFound)
if (manager) return manager
// continue to parent folder
}
printNoLockfilesError();
process.exit(1);
/*
const packageLockExists = fs.existsSync(
join(appRootPath, "package-lock.json"), // npm
)
const shrinkWrapExists = fs.existsSync(
join(appRootPath, "npm-shrinkwrap.json"), // old npm
)
const yarnLockExists = fs.existsSync(join(appRootPath, "yarn.lock"))
const pnpmLockExists = fs.existsSync(
join(appRootPath, "pnpm-lock.yaml"),
)
const oldPnpmLockExists = fs.existsSync(
join(appRootPath, "shrinkwrap.yaml"), // old pnpm. lockfileVersion < 5
)
if (isDebug) {
console.dir({
packageLockExists,
shrinkWrapExists,
yarnLockExists,
pnpmLockExists,
oldPnpmLockExists,
})
}
if ((packageLockExists || shrinkWrapExists) && yarnLockExists) {
if (overridePackageManager) {
return overridePackageManager
} else {
printSelectingDefaultMessage()
return shrinkWrapExists ? "npm-shrinkwrap" : "npm"
}
} else if (packageLockExists || shrinkWrapExists) {
if (overridePackageManager === "yarn") {
printNoYarnLockfileError()
process.exit(1)
} else {
return shrinkWrapExists ? "npm-shrinkwrap" : "npm"
}
} else if (yarnLockExists || findYarnWorkspaceRoot()) {
return "yarn"
} else if (isFileInPnpmRoot(appRootPath, "pnpm-lock.yaml")) {
// (fs.existsSync(join(appRootPath, "pnpm-lock.yaml"))) {
return "pnpm"
} else {
printNoLockfilesError()
process.exit(1)
}
throw Error()
*/
}