fix: hooks (#5534)

This commit is contained in:
Zoltan Kochan
2022-10-20 22:54:54 +03:00
committed by GitHub
parent 1d29fed2eb
commit 0fe9272158
3 changed files with 41 additions and 5 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/hooks.read-package-hook": patch
---
The custom hooks should be executed after the peer dependency patcher hook.

View File

@@ -40,6 +40,11 @@ export function createReadPackageHook (
if (!isEmpty(packageExtensions ?? {})) {
hooks.push(createPackageExtender(packageExtensions!))
}
if (Array.isArray(readPackageHook)) {
hooks.push(...readPackageHook)
} else if (readPackageHook) {
hooks.push(readPackageHook)
}
if (
peerDependencyRules != null &&
(
@@ -50,11 +55,6 @@ export function createReadPackageHook (
) {
hooks.push(createPeerDependencyPatcher(peerDependencyRules))
}
if (Array.isArray(readPackageHook)) {
hooks.push(...readPackageHook)
} else if (readPackageHook) {
hooks.push(readPackageHook)
}
if (hooks.length === 0) {
return undefined

View File

@@ -14,3 +14,34 @@ test('createReadPackageHook() is passing directory to all hooks', async () => {
expect(hook1).toBeCalledWith(manifest, dir)
expect(hook2).toBeCalledWith(manifest, dir)
})
test('createReadPackageHook() runs the custom hook before the peer rules hook', async () => {
const hook = jest.fn((manifest) => ({
...manifest,
dependencies: { ...manifest.peerDependencies },
}))
const readPackageHook = createReadPackageHook({
ignoreCompatibilityDb: true,
lockfileDir: '/foo',
readPackageHook: [hook],
peerDependencyRules: {
allowAny: ['*'],
},
})
const manifest = {
peerDependencies: {
react: '16',
},
}
const dir = '/bar'
const updatedManifest = await readPackageHook!(manifest, dir)
expect(hook).toBeCalledWith(manifest, dir)
expect(updatedManifest).toStrictEqual({
dependencies: {
react: '16',
},
peerDependencies: {
react: '*',
},
})
})