@@ -3,6 +3,7 @@ import { PackageDetails, getPatchDetailsFromCliString } from "./PackageDetails"
3
3
import { PackageManager , detectPackageManager } from "./detectPackageManager"
4
4
import { readFileSync , existsSync } from "fs-extra"
5
5
import { parse as parseYarnLockFile } from "@yarnpkg/lockfile"
6
+ import yaml from "yaml"
6
7
import findWorkspaceRoot from "find-yarn-workspace-root"
7
8
import { getPackageVersion } from "./getPackageVersion"
8
9
@@ -27,22 +28,37 @@ export function getPackageResolution({
27
28
if ( ! existsSync ( lockFilePath ) ) {
28
29
throw new Error ( "Can't find yarn.lock file" )
29
30
}
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
+ }
33
47
}
34
48
35
49
const installedVersion = getPackageVersion (
36
50
join ( resolve ( appPath , packageDetails . path ) , "package.json" ) ,
37
51
)
38
52
39
- const entries = Object . entries ( appLockFile . object ) . filter (
53
+ const entries = Object . entries ( appLockFile ) . filter (
40
54
( [ k , v ] ) =>
41
55
k . startsWith ( packageDetails . name + "@" ) &&
56
+ // @ts -ignore
42
57
v . version === installedVersion ,
43
58
)
44
59
45
60
const resolutions = entries . map ( ( [ _ , v ] ) => {
61
+ // @ts -ignore
46
62
return v . resolved
47
63
} )
48
64
@@ -70,6 +86,10 @@ export function getPackageResolution({
70
86
return `file:${ resolve ( appPath , resolution . slice ( "file:" . length ) ) } `
71
87
}
72
88
89
+ if ( resolution . startsWith ( "npm:" ) ) {
90
+ return resolution . replace ( "npm:" , "" )
91
+ }
92
+
73
93
return resolution
74
94
} else {
75
95
const lockfile = require ( join (
0 commit comments