Skip to content

Add yarn berry compatibility #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"rimraf": "^2.6.3",
"semver": "^5.6.0",
"slash": "^2.0.0",
"tmp": "^0.0.33"
"tmp": "^0.0.33",
"yaml": "^1.10.2"
},
"files": [
"index.js",
Expand Down
28 changes: 24 additions & 4 deletions src/getPackageResolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PackageDetails, getPatchDetailsFromCliString } from "./PackageDetails"
import { PackageManager, detectPackageManager } from "./detectPackageManager"
import { readFileSync, existsSync } from "fs-extra"
import { parse as parseYarnLockFile } from "@yarnpkg/lockfile"
import yaml from "yaml"
import findWorkspaceRoot from "find-yarn-workspace-root"
import { getPackageVersion } from "./getPackageVersion"

Expand All @@ -27,22 +28,37 @@ export function getPackageResolution({
if (!existsSync(lockFilePath)) {
throw new Error("Can't find yarn.lock file")
}
const appLockFile = parseYarnLockFile(readFileSync(lockFilePath).toString())
if (appLockFile.type !== "success") {
throw new Error("Can't parse lock file")
const lockFileString = readFileSync(lockFilePath).toString()
let appLockFile
if (lockFileString.includes("yarn lockfile v1")) {
const parsedYarnLockFile = parseYarnLockFile(lockFileString)
if (parsedYarnLockFile.type !== "success") {
throw new Error("Could not parse yarn v1 lock file")
} else {
appLockFile = parsedYarnLockFile.object
}
} else {
try {
appLockFile = yaml.parse(lockFileString)
} catch (e) {
console.error(e)
throw new Error("Could not parse yarn v2 lock file")
}
}

const installedVersion = getPackageVersion(
join(resolve(appPath, packageDetails.path), "package.json"),
)

const entries = Object.entries(appLockFile.object).filter(
const entries = Object.entries(appLockFile).filter(
([k, v]) =>
k.startsWith(packageDetails.name + "@") &&
// @ts-ignore
v.version === installedVersion,
)

const resolutions = entries.map(([_, v]) => {
// @ts-ignore
return v.resolved
})

Expand Down Expand Up @@ -70,6 +86,10 @@ export function getPackageResolution({
return `file:${resolve(appPath, resolution.slice("file:".length))}`
}

if (resolution.startsWith("npm:")) {
return resolution.replace("npm:", "")
}

return resolution
} else {
const lockfile = require(join(
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5957,6 +5957,11 @@ yaml@^1.10.0:
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.0.tgz#3b593add944876077d4d683fee01081bd9fff31e"
integrity sha512-yr2icI4glYaNG+KWONODapy2/jDdMSDnrONSjblABjD9B4Z5LgiircSt8m8sRZFNi08kG9Sm0uSHtEmP3zaEGg==

yaml@^1.10.2:
version "1.10.2"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.2.tgz#2301c5ffbf12b467de8da2333a459e29e7920e4b"
integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==

[email protected]:
version "10.1.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8"
Expand Down