-
-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathdetectPackageManager.ts
More file actions
70 lines (64 loc) Β· 1.95 KB
/
detectPackageManager.ts
File metadata and controls
70 lines (64 loc) Β· 1.95 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
import fs from "fs-extra"
import { join } from "./path"
import pc from "picocolors"
import process from "process"
import findWorkspaceRoot from "find-yarn-workspace-root"
export type PackageManager = "yarn" | "npm" | "npm-shrinkwrap"
function printNoYarnLockfileError() {
console.log(`
${pc.red(pc.bold("**ERROR**"))} ${pc.red(
`The --use-yarn option was specified but there is no yarn.lock file`,
)}
`)
}
function printNoLockfilesError() {
console.log(`
${pc.red(pc.bold("**ERROR**"))} ${pc.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(
`${pc.bold("patch-package")}: you have both yarn.lock and package-lock.json
Defaulting to using ${pc.bold("npm")}
You can override this setting by passing --use-yarn or deleting
package-lock.json if you don't need it
`,
)
}
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(appRootPath, "yarn.lock"))
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 || findWorkspaceRoot()) {
return "yarn"
} else {
printNoLockfilesError()
process.exit(1)
}
throw Error()
}