1
1
import fs from "fs-extra"
2
2
import { join } from "./path"
3
- import path from "path"
3
+ // import path from "path"
4
4
import chalk from "chalk"
5
5
import process from "process"
6
- import findWorkspaceRoot from "find-yarn-workspace-root"
6
+ // import findYarnWorkspaceRoot from "find-yarn-workspace-root"
7
7
8
8
export type PackageManager = "yarn" | "npm" | "npm-shrinkwrap" | "pnpm"
9
9
10
+ //const isVerbose = global.patchPackageIsVerbose
11
+ const isDebug = global . patchPackageIsDebug
12
+
13
+ /*
10
14
function printNoYarnLockfileError() {
11
15
console.error(`
12
16
${chalk.red.bold("**ERROR**")} ${chalk.red(
13
17
`The --use-yarn option was specified but there is no yarn.lock file`,
14
18
)}
15
19
`)
16
20
}
21
+ */
17
22
18
23
function printNoLockfilesError ( ) {
19
24
console . error ( `
@@ -26,6 +31,7 @@ dependencies.`,
26
31
` )
27
32
}
28
33
34
+ /*
29
35
function printSelectingDefaultMessage() {
30
36
console.info(
31
37
`${chalk.bold(
@@ -37,7 +43,9 @@ package-lock.json if you don't need it
37
43
`,
38
44
)
39
45
}
46
+ */
40
47
48
+ /*
41
49
function isFileInPnpmRoot(rootPath: string, filename: string): boolean {
42
50
const osRoot = path.parse(rootPath).root
43
51
@@ -59,18 +67,104 @@ function isFileInPnpmRoot(rootPath: string, filename: string): boolean {
59
67
60
68
return false
61
69
}
70
+ */
71
+
72
+ function pickManager ( managersFound ) {
73
+ if ( managersFound . includes ( "yarn" ) ) return "yarn"
74
+ if ( managersFound . includes ( "pnpm" ) ) return "pnpm"
75
+ if ( managersFound . includes ( "npm" ) ) return "npm"
76
+ return null
77
+ }
62
78
63
79
export const detectPackageManager = (
64
80
appRootPath : string ,
65
81
overridePackageManager : PackageManager | null ,
66
82
) : PackageManager => {
83
+ if ( isDebug ) {
84
+ console . log ( `patch-package/detectPackageManager:` )
85
+ console . dir ( {
86
+ appRootPath,
87
+ overridePackageManager,
88
+ } )
89
+ }
90
+
91
+ const managerOfLockName = {
92
+ 'package-lock.json' : 'npm' ,
93
+ 'npm-shrinkwrap.json' : 'npm' ,
94
+ 'yarn.lock' : 'yarn' ,
95
+ 'pnpm-lock.yaml' : 'pnpm' ,
96
+ 'shrinkwrap.yaml' : 'pnpm' ,
97
+ }
98
+
99
+ const pathParts = appRootPath . split ( "/" )
100
+ for ( let depth = pathParts . length ; depth > 0 ; depth -- ) {
101
+ const workspaceCandidate = pathParts . slice ( 0 , depth ) . join ( "/" )
102
+ if ( isDebug ) {
103
+ console . log ( `detectPackageManager: workspaceCandidate: ${ workspaceCandidate } ` )
104
+ }
105
+ // TODO fast path
106
+ //if (overridePackageManager) {
107
+ // ...
108
+ //}
109
+ const lockfilesFound : string [ ] = (
110
+ ( [
111
+ // TODO async
112
+ [ 'package-lock.json' , fs . existsSync ( join ( workspaceCandidate , "package-lock.json" ) ) ] ,
113
+ [ 'npm-shrinkwrap.json' , fs . existsSync ( join ( workspaceCandidate , "npm-shrinkwrap.json" ) ) ] , // rare
114
+ [ 'yarn.lock' , fs . existsSync ( join ( workspaceCandidate , "yarn.lock" ) ) ] ,
115
+ [ 'pnpm-lock.yaml' , fs . existsSync ( join ( workspaceCandidate , "pnpm-lock.yaml" ) ) ] ,
116
+ [ 'shrinkwrap.yaml' , fs . existsSync ( join ( workspaceCandidate , "shrinkwrap.yaml" ) ) ] , // rare
117
+ ] as Array < [ string , boolean ] > )
118
+ . filter ( ( [ _file , exists ] ) => exists )
119
+ . map ( ( [ file , _exists ] ) => file )
120
+ )
121
+ if ( isDebug ) {
122
+ console . log ( `detectPackageManager: lockfilesFound: ${ lockfilesFound . join ( ' ' ) } ` )
123
+ }
124
+ if ( lockfilesFound . length == 0 ) {
125
+ continue
126
+ }
127
+ if ( lockfilesFound . length == 1 ) {
128
+ return managerOfLockName [ lockfilesFound [ 0 ] ]
129
+ }
130
+ // found multiple lockfiles
131
+ const managersFound = lockfilesFound . map ( file => managerOfLockName [ file ] )
132
+ if ( overridePackageManager ) {
133
+ // TODO better. if overridePackageManager is set, we can skip some fs.existsSync calls
134
+ if ( managersFound . includes ( overridePackageManager ) ) {
135
+ return overridePackageManager
136
+ }
137
+ continue
138
+ }
139
+ const manager = pickManager ( managersFound )
140
+ if ( manager ) return manager
141
+ // continue to parent folder
142
+ }
143
+ printNoLockfilesError ( ) ;
144
+ process . exit ( 1 ) ;
145
+ /*
67
146
const packageLockExists = fs.existsSync(
68
- join ( appRootPath , "package-lock.json" ) ,
147
+ join(appRootPath, "package-lock.json"), // npm
69
148
)
70
149
const shrinkWrapExists = fs.existsSync(
71
- join ( appRootPath , "npm-shrinkwrap.json" ) ,
150
+ join(appRootPath, "npm-shrinkwrap.json"), // old npm
72
151
)
73
152
const yarnLockExists = fs.existsSync(join(appRootPath, "yarn.lock"))
153
+ const pnpmLockExists = fs.existsSync(
154
+ join(appRootPath, "pnpm-lock.yaml"),
155
+ )
156
+ const oldPnpmLockExists = fs.existsSync(
157
+ join(appRootPath, "shrinkwrap.yaml"), // old pnpm. lockfileVersion < 5
158
+ )
159
+ if (isDebug) {
160
+ console.dir({
161
+ packageLockExists,
162
+ shrinkWrapExists,
163
+ yarnLockExists,
164
+ pnpmLockExists,
165
+ oldPnpmLockExists,
166
+ })
167
+ }
74
168
if ((packageLockExists || shrinkWrapExists) && yarnLockExists) {
75
169
if (overridePackageManager) {
76
170
return overridePackageManager
@@ -85,7 +179,7 @@ export const detectPackageManager = (
85
179
} else {
86
180
return shrinkWrapExists ? "npm-shrinkwrap" : "npm"
87
181
}
88
- } else if ( yarnLockExists || findWorkspaceRoot ( ) ) {
182
+ } else if (yarnLockExists || findYarnWorkspaceRoot ()) {
89
183
return "yarn"
90
184
} else if (isFileInPnpmRoot(appRootPath, "pnpm-lock.yaml")) {
91
185
// (fs.existsSync(join(appRootPath, "pnpm-lock.yaml"))) {
@@ -95,4 +189,5 @@ export const detectPackageManager = (
95
189
process.exit(1)
96
190
}
97
191
throw Error()
192
+ */
98
193
}
0 commit comments