Skip to content

Commit

Permalink
Merge pull request #434 from Threestup/feat/support-newer-lockfile-ve…
Browse files Browse the repository at this point in the history
…rsions

feat(packageresolution): add lockfileVersion 2+ packages support
  • Loading branch information
orta authored Dec 29, 2022
2 parents 85f6eab + 8999185 commit 59cf2e8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
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

0 comments on commit 59cf2e8

Please sign in to comment.