-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add beforeWorkspaceDependency addition/replacement/removal hooks
#6994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
suhdonghwi
wants to merge
8
commits into
yarnpkg:master
Choose a base branch
from
suhdonghwi:feat/before-workspace-dependency-change-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+347
−1
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d7eb23b
Add before versions of dependency change hooks
217fe06
Run replacement hook when yarn add replaces existing dependency
17f8a02
Replacement hooks receive fromDescriptor and toDescriptor
64f8121
Match docs style with after hooks
eaeca5a
Add integration tests
97f561f
yarn version check -i
3ba93b2
Fix all lint errors
14c15bd
Decline dependent packages update
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| releases: | ||
| "@yarnpkg/plugin-essentials": minor | ||
|
|
||
| declined: | ||
| - "@yarnpkg/plugin-interactive-tools" | ||
| - "@yarnpkg/plugin-typescript" | ||
| - "@yarnpkg/cli" | ||
223 changes: 223 additions & 0 deletions
223
packages/acceptance-tests/pkg-tests-specs/sources/features/beforeHooks.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| import {PortablePath, npath, xfs} from '@yarnpkg/fslib'; | ||
| import {stringifySyml} from '@yarnpkg/parsers'; | ||
|
|
||
| describe(`Features`, () => { | ||
| describe(`Before Hooks`, () => { | ||
| describe(`beforeWorkspaceDependencyAddition`, () => { | ||
| test( | ||
| `it should allow plugins to modify descriptor before addition`, | ||
| makeTemporaryEnv({}, async ({path, run}) => { | ||
| const pluginPath = npath.toPortablePath(require.resolve(`@yarnpkg/monorepo/scripts/plugin-before-hooks-test.js`)); | ||
| const pluginContent = await xfs.readFilePromise(pluginPath); | ||
| await xfs.writeFilePromise(`${path}/plugin-before-hooks.js` as PortablePath, pluginContent); | ||
|
|
||
| await xfs.writeFilePromise(`${path}/.yarnrc.yml` as PortablePath, stringifySyml({ | ||
| plugins: [`./plugin-before-hooks.js`], | ||
| })); | ||
|
|
||
| await run(`add`, `no-deps`); | ||
|
|
||
| await expect(xfs.readJsonPromise(`${path}/package.json` as PortablePath)).resolves.toMatchObject({ | ||
| dependencies: { | ||
| [`no-deps`]: `^1.0.0`, | ||
| }, | ||
| }); | ||
| }), | ||
| ); | ||
|
|
||
| test( | ||
| `it should work with dev dependencies`, | ||
| makeTemporaryEnv({}, async ({path, run}) => { | ||
| const pluginPath = npath.toPortablePath(require.resolve(`@yarnpkg/monorepo/scripts/plugin-before-hooks-test.js`)); | ||
| const pluginContent = await xfs.readFilePromise(pluginPath); | ||
| await xfs.writeFilePromise(`${path}/plugin-before-hooks.js` as PortablePath, pluginContent); | ||
|
|
||
| await xfs.writeFilePromise(`${path}/.yarnrc.yml` as PortablePath, stringifySyml({ | ||
| plugins: [`./plugin-before-hooks.js`], | ||
| })); | ||
|
|
||
| await run(`add`, `no-deps`, `-D`); | ||
|
|
||
| await expect(xfs.readJsonPromise(`${path}/package.json` as PortablePath)).resolves.toMatchObject({ | ||
| devDependencies: { | ||
| [`no-deps`]: `^1.0.0`, | ||
| }, | ||
| }); | ||
| }), | ||
| ); | ||
|
|
||
| test( | ||
| `it should not affect other packages`, | ||
| makeTemporaryEnv({}, async ({path, run}) => { | ||
| const pluginPath = npath.toPortablePath(require.resolve(`@yarnpkg/monorepo/scripts/plugin-before-hooks-test.js`)); | ||
| const pluginContent = await xfs.readFilePromise(pluginPath); | ||
| await xfs.writeFilePromise(`${path}/plugin-before-hooks.js` as PortablePath, pluginContent); | ||
|
|
||
| await xfs.writeFilePromise(`${path}/.yarnrc.yml` as PortablePath, stringifySyml({ | ||
| plugins: [`./plugin-before-hooks.js`], | ||
| })); | ||
|
|
||
| await run(`add`, `one-fixed-dep`); | ||
|
|
||
| await expect(xfs.readJsonPromise(`${path}/package.json` as PortablePath)).resolves.toMatchObject({ | ||
| dependencies: { | ||
| [`one-fixed-dep`]: `^2.0.0`, | ||
| }, | ||
| }); | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| describe(`beforeWorkspaceDependencyReplacement`, () => { | ||
| test( | ||
| `it should allow plugins to modify descriptor before replacement via yarn add`, | ||
| makeTemporaryEnv( | ||
| { | ||
| dependencies: { | ||
| 'no-deps': `^1.0.0`, | ||
| }, | ||
| }, | ||
| async ({path, run}) => { | ||
| const pluginPath = npath.toPortablePath(require.resolve(`@yarnpkg/monorepo/scripts/plugin-before-hooks-test.js`)); | ||
| const pluginContent = await xfs.readFilePromise(pluginPath); | ||
| await xfs.writeFilePromise(`${path}/plugin-before-hooks.js` as PortablePath, pluginContent); | ||
|
|
||
| await xfs.writeFilePromise(`${path}/.yarnrc.yml` as PortablePath, stringifySyml({ | ||
| plugins: [`./plugin-before-hooks.js`], | ||
| })); | ||
|
|
||
| await run(`add`, `no-deps@^1.5.0`); | ||
|
|
||
| await expect(xfs.readJsonPromise(`${path}/package.json` as PortablePath)).resolves.toMatchObject({ | ||
| dependencies: { | ||
| [`no-deps`]: `^2.0.0`, | ||
| }, | ||
| }); | ||
| }, | ||
| ), | ||
| ); | ||
|
|
||
| test( | ||
| `it should allow plugins to modify descriptor before replacement via yarn up`, | ||
| makeTemporaryEnv( | ||
| { | ||
| dependencies: { | ||
| 'no-deps': `^1.0.0`, | ||
| }, | ||
| }, | ||
| async ({path, run}) => { | ||
| const pluginPath = npath.toPortablePath(require.resolve(`@yarnpkg/monorepo/scripts/plugin-before-hooks-test.js`)); | ||
| const pluginContent = await xfs.readFilePromise(pluginPath); | ||
| await xfs.writeFilePromise(`${path}/plugin-before-hooks.js` as PortablePath, pluginContent); | ||
|
|
||
| await xfs.writeFilePromise(`${path}/.yarnrc.yml` as PortablePath, stringifySyml({ | ||
| plugins: [`./plugin-before-hooks.js`], | ||
| })); | ||
|
|
||
| await run(`up`, `no-deps`); | ||
|
|
||
| await expect(xfs.readJsonPromise(`${path}/package.json` as PortablePath)).resolves.toMatchObject({ | ||
| dependencies: { | ||
| [`no-deps`]: `^2.0.0`, | ||
| }, | ||
| }); | ||
| }, | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| describe(`beforeWorkspaceDependencyRemoval`, () => { | ||
| test( | ||
| `it should allow plugins to block removal by throwing`, | ||
| makeTemporaryEnv( | ||
| { | ||
| dependencies: { | ||
| 'no-deps': `^2.0.0`, | ||
| }, | ||
| }, | ||
| async ({path, run}) => { | ||
| const pluginPath = npath.toPortablePath(require.resolve(`@yarnpkg/monorepo/scripts/plugin-before-hooks-test.js`)); | ||
| const pluginContent = await xfs.readFilePromise(pluginPath); | ||
| await xfs.writeFilePromise(`${path}/plugin-before-hooks.js` as PortablePath, pluginContent); | ||
|
|
||
| await xfs.writeFilePromise(`${path}/.yarnrc.yml` as PortablePath, stringifySyml({ | ||
| plugins: [`./plugin-before-hooks.js`], | ||
| })); | ||
|
|
||
| await expect(run(`remove`, `no-deps`)).rejects.toThrow(); | ||
|
|
||
| await expect(xfs.readJsonPromise(`${path}/package.json` as PortablePath)).resolves.toMatchObject({ | ||
| dependencies: { | ||
| 'no-deps': `^2.0.0`, | ||
| }, | ||
| }); | ||
| }, | ||
| ), | ||
| ); | ||
|
|
||
| test( | ||
| `it should allow removal of other packages`, | ||
| makeTemporaryEnv( | ||
| { | ||
| dependencies: { | ||
| 'one-fixed-dep': `^1.0.0`, | ||
| }, | ||
| }, | ||
| async ({path, run}) => { | ||
| const pluginPath = npath.toPortablePath(require.resolve(`@yarnpkg/monorepo/scripts/plugin-before-hooks-test.js`)); | ||
| const pluginContent = await xfs.readFilePromise(pluginPath); | ||
| await xfs.writeFilePromise(`${path}/plugin-before-hooks.js` as PortablePath, pluginContent); | ||
|
|
||
| await xfs.writeFilePromise(`${path}/.yarnrc.yml` as PortablePath, stringifySyml({ | ||
| plugins: [`./plugin-before-hooks.js`], | ||
| })); | ||
|
|
||
| await run(`remove`, `one-fixed-dep`); | ||
|
|
||
| const manifest = await xfs.readJsonPromise(`${path}/package.json` as PortablePath); | ||
| expect(manifest.dependencies).toBeUndefined(); | ||
| }, | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| describe(`Multiple hooks interaction`, () => { | ||
| test( | ||
| `it should handle both addition and replacement hooks in yarn add`, | ||
| makeTemporaryEnv( | ||
| { | ||
| dependencies: { | ||
| 'no-deps': `^1.0.0`, | ||
| }, | ||
| }, | ||
| async ({path, run}) => { | ||
| const pluginPath = npath.toPortablePath(require.resolve(`@yarnpkg/monorepo/scripts/plugin-before-hooks-test.js`)); | ||
| const pluginContent = await xfs.readFilePromise(pluginPath); | ||
| await xfs.writeFilePromise(`${path}/plugin-before-hooks.js` as PortablePath, pluginContent); | ||
|
|
||
| await xfs.writeFilePromise(`${path}/.yarnrc.yml` as PortablePath, stringifySyml({ | ||
| plugins: [`./plugin-before-hooks.js`], | ||
| })); | ||
|
|
||
| await run(`add`, `no-deps@^1.5.0`); | ||
|
|
||
| await expect(xfs.readJsonPromise(`${path}/package.json` as PortablePath)).resolves.toMatchObject({ | ||
| dependencies: { | ||
| [`no-deps`]: `^2.0.0`, | ||
| }, | ||
| }); | ||
|
|
||
| await run(`add`, `one-fixed-dep`); | ||
|
|
||
| await expect(xfs.readJsonPromise(`${path}/package.json` as PortablePath)).resolves.toMatchObject({ | ||
| dependencies: { | ||
| [`no-deps`]: `^2.0.0`, | ||
| [`one-fixed-dep`]: expect.any(String), | ||
| }, | ||
| }); | ||
| }, | ||
| ), | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -194,6 +194,34 @@ export default class AddCommand extends BaseCommand { | |
| }); | ||
|
|
||
| const results = await Promise.all(targetList.map(async target => { | ||
| const current = workspace.manifest[target].get(request.identHash); | ||
| const isReplacement = typeof current !== `undefined`; | ||
|
|
||
| for (const plugin of configuration.plugins.values()) { | ||
| const hooks = plugin.hooks as Hooks; | ||
|
|
||
| if (isReplacement) { | ||
| if (!hooks?.beforeWorkspaceDependencyReplacement) | ||
| continue; | ||
|
|
||
| await hooks.beforeWorkspaceDependencyReplacement( | ||
| workspace, | ||
| target, | ||
| current, | ||
| request, | ||
| ); | ||
| } else { | ||
| if (!hooks?.beforeWorkspaceDependencyAddition) | ||
| continue; | ||
|
|
||
| await hooks.beforeWorkspaceDependencyAddition( | ||
| workspace, | ||
| target, | ||
| request, | ||
| ); | ||
| } | ||
| } | ||
|
Comment on lines
+200
to
+223
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use |
||
|
|
||
| const suggestedDescriptors = await suggestUtils.getSuggestedDescriptors(request, {project, workspace, cache, fixed, target, modifier, strategies, maxResults}); | ||
| return {request, suggestedDescriptors, target}; | ||
| })); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| module.exports = { | ||
| name: `@yarnpkg/plugin-before-hooks-test`, | ||
| factory: () => { | ||
| return { | ||
| hooks: { | ||
| beforeWorkspaceDependencyAddition: async (workspace, target, descriptor) => { | ||
| if (descriptor.name === `no-deps`) { | ||
| descriptor.range = `^1.0.0`; | ||
| } | ||
| }, | ||
|
|
||
| beforeWorkspaceDependencyReplacement: async (workspace, target, fromDescriptor, toDescriptor) => { | ||
| if (toDescriptor.name === `no-deps`) { | ||
| toDescriptor.range = `^2.0.0`; | ||
| } | ||
| }, | ||
|
|
||
| beforeWorkspaceDependencyRemoval: async (workspace, target, descriptor) => { | ||
| if (descriptor.name === `no-deps`) { | ||
| throw new Error(`Cannot remove no-deps - it is protected`); | ||
| } | ||
| }, | ||
| }, | ||
| }; | ||
| }, | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yarnpkg/clialso needs a release