MCPcopy Index your code
hub / github.com/ds300/patch-package / getPackageResolution

Function getPackageResolution

src/getPackageResolution.ts:11–123  ·  view source on GitHub ↗
({
  packageDetails,
  packageManager,
  appPath,
}: {
  packageDetails: PackageDetails
  packageManager: PackageManager
  appPath: string
})

Source from the content-addressed store, hash-verified

9import { coerceSemVer } from "./coerceSemVer"
10
11export function getPackageResolution({
12 packageDetails,
13 packageManager,
14 appPath,
15}: {
16 packageDetails: PackageDetails
17 packageManager: PackageManager
18 appPath: string
19}) {
20 if (packageManager === "yarn") {
21 let lockFilePath = "yarn.lock"
22 if (!existsSync(lockFilePath)) {
23 const workspaceRoot = findWorkspaceRoot()
24 if (!workspaceRoot) {
25 throw new Error("Can't find yarn.lock file")
26 }
27 lockFilePath = join(workspaceRoot, "yarn.lock")
28 }
29 if (!existsSync(lockFilePath)) {
30 throw new Error("Can't find yarn.lock file")
31 }
32 const lockFileString = readFileSync(lockFilePath).toString()
33 let appLockFile
34 if (lockFileString.includes("yarn lockfile v1")) {
35 const parsedYarnLockFile = parseYarnLockFile(lockFileString)
36 if (parsedYarnLockFile.type !== "success") {
37 throw new Error("Could not parse yarn v1 lock file")
38 } else {
39 appLockFile = parsedYarnLockFile.object
40 }
41 } else {
42 try {
43 appLockFile = yaml.parse(lockFileString)
44 } catch (e) {
45 console.log(e)
46 throw new Error("Could not parse yarn v2 lock file")
47 }
48 }
49
50 const installedVersion = getPackageVersion(
51 join(resolve(appPath, packageDetails.path), "package.json"),
52 )
53
54 const entries = Object.entries(appLockFile).filter(
55 ([k, v]) =>
56 k.startsWith(packageDetails.name + "@") &&
57 // @ts-ignore
58 coerceSemVer(v.version) === coerceSemVer(installedVersion),
59 )
60
61 const resolutions = entries.map(([_, v]) => {
62 // @ts-ignore
63 return v.resolved
64 })
65
66 if (resolutions.length === 0) {
67 throw new Error(
68 `\`${packageDetails.pathSpecifier}\`'s installed version is ${installedVersion} but a lockfile entry for it couldn't be found. Your lockfile is likely to be corrupt or you forgot to reinstall your packages.`,

Callers 2

makePatchFunction · 0.90

Calls 4

joinFunction · 0.90
getPackageVersionFunction · 0.90
resolveFunction · 0.90
coerceSemVerFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…