Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [email protected]

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