Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(packageresolution): add lockfileVersion 2+ packages support #434

Merged
merged 14 commits into from
Dec 29, 2022
32 changes: 32 additions & 0 deletions integration-tests/lockfile/lockfile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# make sure errors stop the script
set -e

echo "add patch-package"
npm i $1
alias patch-package=./node_modules/.bin/patch-package

echo "Add left-pad"
npm i left-pad@1.3.0

testLockFile() {
echo "Version test $1"
npm i --lockfile-version $1

echo "cleanup patches"
npx rimraf patches

echo "replace pad with yarn in left-pad/index.js"
npx replace pad npm node_modules/left-pad/index.js

echo "patch-package should run"
patch-package left-pad

echo "check that the patch is created"
test -f patches/left-pad+1.3.0.patch || exit 1
}

echo "test lockfile v2"
testLockFile 2

echo "test lockfile v3"
testLockFile 3
2 changes: 2 additions & 0 deletions integration-tests/lockfile/lockfile.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { runIntegrationTest } from "../runIntegrationTest"
runIntegrationTest({ projectName: "lockfile", shouldProduceSnapshots: false })
11 changes: 11 additions & 0 deletions integration-tests/lockfile/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "lockfile",
"version": "1.0.0",
"description": "integration test for patch-package",
"main": "index.js",
"author": "anas10",
"license": "ISC",
"dependencies": {
"left-pad": "1.3.0"
}
}
8 changes: 8 additions & 0 deletions integration-tests/lockfile/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"left-pad@^1.1.3":
"integrity" "sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA=="
"resolved" "https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz"
"version" "1.3.0"
17 changes: 11 additions & 6 deletions src/getPackageResolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,17 @@ export function getPackageResolution({
}
}
lockFileStack.reverse()
const relevantStackEntry = lockFileStack.find(
(entry) =>
entry.dependencies && packageDetails.name in entry.dependencies,
)
const pkg = relevantStackEntry.dependencies[packageDetails.name]
const relevantStackEntry = lockFileStack.find((entry) => {
if (entry.dependencies) {
return entry.dependencies && packageDetails.name in entry.dependencies
} else if (entry.packages) {
return entry.packages && packageDetails.path in entry.packages
}
throw new Error("Cannot find dependencies or packages in lockfile")
})
const pkg = relevantStackEntry.dependencies
? relevantStackEntry.dependencies[packageDetails.name]
: relevantStackEntry.packages[packageDetails.path]
return pkg.resolved || pkg.version || pkg.from
}
}
Expand All @@ -120,7 +126,6 @@ if (require.main === module) {
if (!packageDetails) {
console.error(`Can't find package ${process.argv[2]}`)
process.exit(1)
throw new Error()
}
console.log(
getPackageResolution({
Expand Down