Skip to content

Commit

Permalink
Merge pull request #363 from maman/feature/add-yarn-berry-compatibility
Browse files Browse the repository at this point in the history
Add yarn berry compatibility
  • Loading branch information
orta authored Sep 28, 2022
2 parents 7c87e05 + b80d96a commit bb628da
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,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 @@ -5975,6 +5975,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==

yargs-parser@10.x:
version "10.1.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8"
Expand Down

0 comments on commit bb628da

Please sign in to comment.