-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use lockfiles to determine package resolutions.
- Loading branch information
Showing
7 changed files
with
133 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { join, resolve } from "./path" | ||
import { PackageDetails, getPatchDetailsFromCliString } from "./PackageDetails" | ||
import { PackageManager, detectPackageManager } from "./detectPackageManager" | ||
import { readFileSync } from "fs-extra" | ||
import { parse as parseYarnLockFile } from "@yarnpkg/lockfile" | ||
|
||
export function getPackageResolution({ | ||
packageDetails, | ||
packageManager, | ||
appPath, | ||
}: { | ||
packageDetails: PackageDetails | ||
packageManager: PackageManager | ||
appPath: string | ||
}) { | ||
if (packageManager === "yarn") { | ||
const appLockFile = parseYarnLockFile(readFileSync("yarn.lock").toString()) | ||
if (appLockFile.type !== "success") { | ||
throw new Error("Can't parse lock file") | ||
} | ||
|
||
const installedVersion = require(join( | ||
resolve(appPath, packageDetails.path), | ||
"package.json", | ||
)).version as string | ||
|
||
const entries = Object.entries(appLockFile.object).filter( | ||
([k, v]) => | ||
k.startsWith(packageDetails.name + "@") && | ||
v.version === installedVersion, | ||
) | ||
|
||
const resolutions = entries.map(([_, v]) => { | ||
return v.resolved | ||
}) | ||
|
||
if (resolutions.length === 0) { | ||
throw new Error( | ||
`Can't find lockfile entry for ${packageDetails.pathSpecifier}`, | ||
) | ||
} | ||
|
||
if (new Set(resolutions).size !== 1) { | ||
console.warn( | ||
`Ambigious lockfile entries for ${ | ||
packageDetails.pathSpecifier | ||
}. Using version ${installedVersion}`, | ||
) | ||
return installedVersion | ||
} | ||
|
||
if (resolutions[0]) { | ||
return resolutions[0] | ||
} | ||
|
||
const resolution = entries[0][0].slice(packageDetails.name.length + 1) | ||
|
||
// resolve relative file path | ||
if (resolution.startsWith("file:.")) { | ||
return `file:${resolve(appPath, resolution.slice("file:".length))}` | ||
} | ||
|
||
return resolution | ||
} else { | ||
const lockfile = require(join( | ||
appPath, | ||
packageManager === "npm-shrinkwrap" | ||
? "npm-shrinkwrap.json" | ||
: "package-lock.json", | ||
)) | ||
const lockFileStack = [lockfile] | ||
for (const name of packageDetails.packageNames.slice(0, -1)) { | ||
const child = lockFileStack[0].dependencies | ||
if (child && name in child) { | ||
lockFileStack.push(child[name]) | ||
} | ||
} | ||
lockFileStack.reverse() | ||
const relevantStackEntry = lockFileStack.find( | ||
entry => entry.dependencies && packageDetails.name in entry.dependencies, | ||
) | ||
return relevantStackEntry.dependencies[packageDetails.name].resolved | ||
} | ||
} | ||
|
||
if (require.main === module) { | ||
const packageDetails = getPatchDetailsFromCliString(process.argv[2]) | ||
if (!packageDetails) { | ||
console.error(`Can't find package ${process.argv[2]}`) | ||
process.exit(1) | ||
throw new Error() | ||
} | ||
console.log( | ||
getPackageResolution({ | ||
appPath: process.cwd(), | ||
packageDetails, | ||
packageManager: detectPackageManager(process.cwd(), null), | ||
}), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
declare module "@yarnpkg/lockfile" { | ||
export function parse( | ||
s: string, | ||
): { | ||
type: "success" | "error" | ||
object: { | ||
[identifier: string]: { | ||
resolved?: string | ||
version: string | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters