Skip to content

Commit bb628da

Browse files
authored
Merge pull request #363 from maman/feature/add-yarn-berry-compatibility
Add yarn berry compatibility
2 parents 7c87e05 + b80d96a commit bb628da

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
"rimraf": "^2.6.3",
8585
"semver": "^5.6.0",
8686
"slash": "^2.0.0",
87-
"tmp": "^0.0.33"
87+
"tmp": "^0.0.33",
88+
"yaml": "^1.10.2"
8889
},
8990
"files": [
9091
"index.js",

src/getPackageResolution.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +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 yaml from "yaml"
67
import findWorkspaceRoot from "find-yarn-workspace-root"
78
import { getPackageVersion } from "./getPackageVersion"
89

@@ -27,22 +28,37 @@ export function getPackageResolution({
2728
if (!existsSync(lockFilePath)) {
2829
throw new Error("Can't find yarn.lock file")
2930
}
30-
const appLockFile = parseYarnLockFile(readFileSync(lockFilePath).toString())
31-
if (appLockFile.type !== "success") {
32-
throw new Error("Can't parse lock file")
31+
const lockFileString = readFileSync(lockFilePath).toString()
32+
let appLockFile
33+
if (lockFileString.includes("yarn lockfile v1")) {
34+
const parsedYarnLockFile = parseYarnLockFile(lockFileString)
35+
if (parsedYarnLockFile.type !== "success") {
36+
throw new Error("Could not parse yarn v1 lock file")
37+
} else {
38+
appLockFile = parsedYarnLockFile.object
39+
}
40+
} else {
41+
try {
42+
appLockFile = yaml.parse(lockFileString)
43+
} catch (e) {
44+
console.error(e)
45+
throw new Error("Could not parse yarn v2 lock file")
46+
}
3347
}
3448

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

39-
const entries = Object.entries(appLockFile.object).filter(
53+
const entries = Object.entries(appLockFile).filter(
4054
([k, v]) =>
4155
k.startsWith(packageDetails.name + "@") &&
56+
// @ts-ignore
4257
v.version === installedVersion,
4358
)
4459

4560
const resolutions = entries.map(([_, v]) => {
61+
// @ts-ignore
4662
return v.resolved
4763
})
4864

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

89+
if (resolution.startsWith("npm:")) {
90+
return resolution.replace("npm:", "")
91+
}
92+
7393
return resolution
7494
} else {
7595
const lockfile = require(join(

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -5975,6 +5975,11 @@ yaml@^1.10.0:
59755975
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.0.tgz#3b593add944876077d4d683fee01081bd9fff31e"
59765976
integrity sha512-yr2icI4glYaNG+KWONODapy2/jDdMSDnrONSjblABjD9B4Z5LgiircSt8m8sRZFNi08kG9Sm0uSHtEmP3zaEGg==
59775977

5978+
yaml@^1.10.2:
5979+
version "1.10.2"
5980+
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.2.tgz#2301c5ffbf12b467de8da2333a459e29e7920e4b"
5981+
integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==
5982+
59785983
59795984
version "10.1.0"
59805985
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8"

0 commit comments

Comments
 (0)