Skip to content

Commit d28b55a

Browse files
authored
Add yarn berry compatibility
1 parent 5c2c92b commit d28b55a

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

package.json

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

src/getPackageResolution.ts

+22-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,35 @@ 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+
appLockFile = parseYarnLockFile(lockFileString)
35+
if (appLockFile.type !== "success") {
36+
throw new Error("Can't parse lock file")
37+
}
38+
} else {
39+
try {
40+
appLockFile = yaml.parse(lockFileString)
41+
} catch (e) {
42+
console.error(e)
43+
throw new Error("Can't parse lock file")
44+
}
3345
}
3446

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

39-
const entries = Object.entries(appLockFile.object).filter(
51+
const entries = Object.entries(appLockFile.object || appLockFile).filter(
4052
([k, v]) =>
4153
k.startsWith(packageDetails.name + "@") &&
54+
// @ts-ignore
4255
v.version === installedVersion,
4356
)
4457

4558
const resolutions = entries.map(([_, v]) => {
59+
// @ts-ignore
4660
return v.resolved
4761
})
4862

@@ -70,6 +84,10 @@ export function getPackageResolution({
7084
return `file:${resolve(appPath, resolution.slice("file:".length))}`
7185
}
7286

87+
if (resolution.startsWith("npm:")) {
88+
return resolution.replace("npm:", "")
89+
}
90+
7391
return resolution
7492
} else {
7593
const lockfile = require(join(

yarn.lock

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

5960+
yaml@^1.10.2:
5961+
version "1.10.2"
5962+
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.2.tgz#2301c5ffbf12b467de8da2333a459e29e7920e4b"
5963+
integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==
5964+
59605965
59615966
version "10.1.0"
59625967
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8"

0 commit comments

Comments
 (0)