diff --git a/integration-tests/lockfile/lockfile.sh b/integration-tests/lockfile/lockfile.sh new file mode 100755 index 00000000..e7872269 --- /dev/null +++ b/integration-tests/lockfile/lockfile.sh @@ -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 diff --git a/integration-tests/lockfile/lockfile.test.ts b/integration-tests/lockfile/lockfile.test.ts new file mode 100644 index 00000000..7042e98c --- /dev/null +++ b/integration-tests/lockfile/lockfile.test.ts @@ -0,0 +1,2 @@ +import { runIntegrationTest } from "../runIntegrationTest" +runIntegrationTest({ projectName: "lockfile", shouldProduceSnapshots: false }) diff --git a/integration-tests/lockfile/package.json b/integration-tests/lockfile/package.json new file mode 100644 index 00000000..8fb9a92a --- /dev/null +++ b/integration-tests/lockfile/package.json @@ -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" + } +} diff --git a/integration-tests/lockfile/yarn.lock b/integration-tests/lockfile/yarn.lock new file mode 100644 index 00000000..7cc1da5c --- /dev/null +++ b/integration-tests/lockfile/yarn.lock @@ -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" diff --git a/src/getPackageResolution.ts b/src/getPackageResolution.ts index afe6265b..ccfb3612 100644 --- a/src/getPackageResolution.ts +++ b/src/getPackageResolution.ts @@ -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 } } @@ -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({