From d28b55a914111408a05f63bafb18f1c540b93152 Mon Sep 17 00:00:00 2001 From: Achmad Mahardi Date: Fri, 3 Dec 2021 12:57:25 +0000 Subject: [PATCH 1/3] Add yarn berry compatibility --- package.json | 3 ++- src/getPackageResolution.ts | 26 ++++++++++++++++++++++---- yarn.lock | 5 +++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index bf67ee95..2f75a254 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/getPackageResolution.ts b/src/getPackageResolution.ts index 5c8c158b..a8f4de93 100644 --- a/src/getPackageResolution.ts +++ b/src/getPackageResolution.ts @@ -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" @@ -27,22 +28,35 @@ 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")) { + appLockFile = parseYarnLockFile(lockFileString) + if (appLockFile.type !== "success") { + throw new Error("Can't parse lock file") + } + } else { + try { + appLockFile = yaml.parse(lockFileString) + } catch (e) { + console.error(e) + throw new Error("Can't parse lock file") + } } const installedVersion = getPackageVersion( join(resolve(appPath, packageDetails.path), "package.json"), ) - const entries = Object.entries(appLockFile.object).filter( + const entries = Object.entries(appLockFile.object || appLockFile).filter( ([k, v]) => k.startsWith(packageDetails.name + "@") && + // @ts-ignore v.version === installedVersion, ) const resolutions = entries.map(([_, v]) => { + // @ts-ignore return v.resolved }) @@ -70,6 +84,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( diff --git a/yarn.lock b/yarn.lock index 37aa5d06..5ed07624 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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== + yargs-parser@10.x: version "10.1.0" resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8" From b4dad0bee04c7fc864ad13557b8945992ee3011b Mon Sep 17 00:00:00 2001 From: Achmad Mahardi Date: Sun, 5 Dec 2021 22:51:19 +0000 Subject: [PATCH 2/3] FEAT: simplify appLockFile handling --- src/getPackageResolution.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/getPackageResolution.ts b/src/getPackageResolution.ts index a8f4de93..775293b6 100644 --- a/src/getPackageResolution.ts +++ b/src/getPackageResolution.ts @@ -31,9 +31,11 @@ export function getPackageResolution({ const lockFileString = readFileSync(lockFilePath).toString() let appLockFile if (lockFileString.includes("yarn lockfile v1")) { - appLockFile = parseYarnLockFile(lockFileString) - if (appLockFile.type !== "success") { + const parsedYarnLockFile = parseYarnLockFile(lockFileString) + if (parsedYarnLockFile.type !== "success") { throw new Error("Can't parse lock file") + } else { + appLockFile = parsedYarnLockFile.object } } else { try { @@ -48,7 +50,7 @@ export function getPackageResolution({ join(resolve(appPath, packageDetails.path), "package.json"), ) - const entries = Object.entries(appLockFile.object || appLockFile).filter( + const entries = Object.entries(appLockFile).filter( ([k, v]) => k.startsWith(packageDetails.name + "@") && // @ts-ignore From b80d96a3e63d8cbd0df85f3d0898a3e4e805b1d7 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Wed, 28 Sep 2022 09:37:44 +0100 Subject: [PATCH 3/3] Apply suggestions from code review --- src/getPackageResolution.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/getPackageResolution.ts b/src/getPackageResolution.ts index 775293b6..64f539c4 100644 --- a/src/getPackageResolution.ts +++ b/src/getPackageResolution.ts @@ -33,7 +33,7 @@ export function getPackageResolution({ if (lockFileString.includes("yarn lockfile v1")) { const parsedYarnLockFile = parseYarnLockFile(lockFileString) if (parsedYarnLockFile.type !== "success") { - throw new Error("Can't parse lock file") + throw new Error("Could not parse yarn v1 lock file") } else { appLockFile = parsedYarnLockFile.object } @@ -42,7 +42,7 @@ export function getPackageResolution({ appLockFile = yaml.parse(lockFileString) } catch (e) { console.error(e) - throw new Error("Can't parse lock file") + throw new Error("Could not parse yarn v2 lock file") } }