Skip to content

Commit 3545f96

Browse files
committed
fix workspace detection?
check lockfiles in parallel simply pick the first-best match dont verify if the package is actually in the workspace
1 parent a34589b commit 3545f96

File tree

2 files changed

+112
-8
lines changed

2 files changed

+112
-8
lines changed

src/detectPackageManager.ts

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import fs from "fs-extra"
22
import { join } from "./path"
3-
import path from "path"
3+
//import path from "path"
44
import chalk from "chalk"
55
import process from "process"
6-
import findWorkspaceRoot from "find-yarn-workspace-root"
6+
//import findYarnWorkspaceRoot from "find-yarn-workspace-root"
77

88
export type PackageManager = "yarn" | "npm" | "npm-shrinkwrap" | "pnpm"
99

10+
//const isVerbose = global.patchPackageIsVerbose
11+
const isDebug = global.patchPackageIsDebug
12+
13+
/*
1014
function printNoYarnLockfileError() {
1115
console.error(`
1216
${chalk.red.bold("**ERROR**")} ${chalk.red(
1317
`The --use-yarn option was specified but there is no yarn.lock file`,
1418
)}
1519
`)
1620
}
21+
*/
1722

1823
function printNoLockfilesError() {
1924
console.error(`
@@ -26,6 +31,7 @@ dependencies.`,
2631
`)
2732
}
2833

34+
/*
2935
function printSelectingDefaultMessage() {
3036
console.info(
3137
`${chalk.bold(
@@ -37,7 +43,9 @@ package-lock.json if you don't need it
3743
`,
3844
)
3945
}
46+
*/
4047

48+
/*
4149
function isFileInPnpmRoot(rootPath: string, filename: string): boolean {
4250
const osRoot = path.parse(rootPath).root
4351
@@ -59,18 +67,104 @@ function isFileInPnpmRoot(rootPath: string, filename: string): boolean {
5967
6068
return false
6169
}
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+
}
6278

6379
export const detectPackageManager = (
6480
appRootPath: string,
6581
overridePackageManager: PackageManager | null,
6682
): 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+
/*
67146
const packageLockExists = fs.existsSync(
68-
join(appRootPath, "package-lock.json"),
147+
join(appRootPath, "package-lock.json"), // npm
69148
)
70149
const shrinkWrapExists = fs.existsSync(
71-
join(appRootPath, "npm-shrinkwrap.json"),
150+
join(appRootPath, "npm-shrinkwrap.json"), // old npm
72151
)
73152
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+
}
74168
if ((packageLockExists || shrinkWrapExists) && yarnLockExists) {
75169
if (overridePackageManager) {
76170
return overridePackageManager
@@ -85,7 +179,7 @@ export const detectPackageManager = (
85179
} else {
86180
return shrinkWrapExists ? "npm-shrinkwrap" : "npm"
87181
}
88-
} else if (yarnLockExists || findWorkspaceRoot()) {
182+
} else if (yarnLockExists || findYarnWorkspaceRoot()) {
89183
return "yarn"
90184
} else if (isFileInPnpmRoot(appRootPath, "pnpm-lock.yaml")) {
91185
// (fs.existsSync(join(appRootPath, "pnpm-lock.yaml"))) {
@@ -95,4 +189,5 @@ export const detectPackageManager = (
95189
process.exit(1)
96190
}
97191
throw Error()
192+
*/
98193
}

src/getPackageResolution.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { PackageDetails, getPatchDetailsFromCliString } from "./PackageDetails"
33
import { PackageManager, detectPackageManager } from "./detectPackageManager"
44
import { readFileSync, existsSync } from "fs-extra"
55
import { parse as parseYarnLockFile } from "@yarnpkg/lockfile"
6-
import findWorkspaceRoot from "find-yarn-workspace-root"
6+
import findYarnWorkspaceRoot from "find-yarn-workspace-root"
77
import { getPackageVersion } from "./getPackageVersion"
88
//import { execSync } from "child_process"
99

@@ -23,7 +23,7 @@ export function getPackageResolution({
2323
}) {
2424

2525
if (isDebug) {
26-
console.log(`getPackageResolution:`)
26+
console.log(`patch-package/getPackageResolution:`)
2727
console.dir({
2828
packageDetails, // dependency
2929
packageManager,
@@ -58,15 +58,24 @@ export function getPackageResolution({
5858
if (packageManager === "yarn") {
5959
let lockFilePath = "yarn.lock"
6060
if (!existsSync(lockFilePath)) {
61-
const workspaceRoot = findWorkspaceRoot()
61+
if (isDebug) {
62+
console.log(`patch-package/getPackageResolution: yarn.lock is not here, trying findYarnWorkspaceRoot`)
63+
}
64+
const workspaceRoot = findYarnWorkspaceRoot()
6265
if (!workspaceRoot) {
6366
throw new Error("Can't find yarn.lock file")
6467
}
68+
if (isDebug) {
69+
console.log(`patch-package/getPackageResolution: found yarn workspace: ${workspaceRoot}`)
70+
}
6571
lockFilePath = join(workspaceRoot, "yarn.lock")
6672
}
6773
if (!existsSync(lockFilePath)) {
6874
throw new Error("Can't find yarn.lock file")
6975
}
76+
if (isDebug) {
77+
console.log(`patch-package/getPackageResolution: found yarn lockfile: ${lockFilePath}`)
78+
}
7079
const appLockFile = parseYarnLockFile(readFileSync(lockFilePath).toString())
7180
if (appLockFile.type !== "success") {
7281
throw new Error("Can't parse lock file")

0 commit comments

Comments
 (0)