Skip to content

Commit 59cf2e8

Browse files
authored
Merge pull request #434 from Threestup/feat/support-newer-lockfile-versions
feat(packageresolution): add lockfileVersion 2+ packages support
2 parents 85f6eab + 8999185 commit 59cf2e8

File tree

5 files changed

+64
-6
lines changed

5 files changed

+64
-6
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# make sure errors stop the script
2+
set -e
3+
4+
echo "add patch-package"
5+
npm i $1
6+
alias patch-package=./node_modules/.bin/patch-package
7+
8+
echo "Add left-pad"
9+
10+
11+
testLockFile() {
12+
echo "Version test $1"
13+
npm i --lockfile-version $1
14+
15+
echo "cleanup patches"
16+
npx rimraf patches
17+
18+
echo "replace pad with yarn in left-pad/index.js"
19+
npx replace pad npm node_modules/left-pad/index.js
20+
21+
echo "patch-package should run"
22+
patch-package left-pad
23+
24+
echo "check that the patch is created"
25+
test -f patches/left-pad+1.3.0.patch || exit 1
26+
}
27+
28+
echo "test lockfile v2"
29+
testLockFile 2
30+
31+
echo "test lockfile v3"
32+
testLockFile 3
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { runIntegrationTest } from "../runIntegrationTest"
2+
runIntegrationTest({ projectName: "lockfile", shouldProduceSnapshots: false })
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "lockfile",
3+
"version": "1.0.0",
4+
"description": "integration test for patch-package",
5+
"main": "index.js",
6+
"author": "anas10",
7+
"license": "ISC",
8+
"dependencies": {
9+
"left-pad": "1.3.0"
10+
}
11+
}

integration-tests/lockfile/yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
"left-pad@^1.1.3":
6+
"integrity" "sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA=="
7+
"resolved" "https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz"
8+
"version" "1.3.0"

src/getPackageResolution.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,17 @@ export function getPackageResolution({
106106
}
107107
}
108108
lockFileStack.reverse()
109-
const relevantStackEntry = lockFileStack.find(
110-
(entry) =>
111-
entry.dependencies && packageDetails.name in entry.dependencies,
112-
)
113-
const pkg = relevantStackEntry.dependencies[packageDetails.name]
109+
const relevantStackEntry = lockFileStack.find((entry) => {
110+
if (entry.dependencies) {
111+
return entry.dependencies && packageDetails.name in entry.dependencies
112+
} else if (entry.packages) {
113+
return entry.packages && packageDetails.path in entry.packages
114+
}
115+
throw new Error("Cannot find dependencies or packages in lockfile")
116+
})
117+
const pkg = relevantStackEntry.dependencies
118+
? relevantStackEntry.dependencies[packageDetails.name]
119+
: relevantStackEntry.packages[packageDetails.path]
114120
return pkg.resolved || pkg.version || pkg.from
115121
}
116122
}
@@ -120,7 +126,6 @@ if (require.main === module) {
120126
if (!packageDetails) {
121127
console.error(`Can't find package ${process.argv[2]}`)
122128
process.exit(1)
123-
throw new Error()
124129
}
125130
console.log(
126131
getPackageResolution({

0 commit comments

Comments
 (0)