Skip to content

Commit d582568

Browse files
marioneblmerceyz
andcommitted
feat(essentials): add preferReuse setting (#4221)
* feat: expose preferReuse and preferCached * chore: add release data * remove preferCached * remove preferCached from config * set all release strategies * add default * add test * remove unused * docs: improve explanation Co-authored-by: Kristoffer K. <merceyz@users.noreply.github.com> * style: inline type predicate Co-authored-by: Kristoffer K. <merceyz@users.noreply.github.com> * refactor: remove unused Co-authored-by: Kristoffer K. <merceyz@users.noreply.github.com> * fix: move preferReuse config * refactor: use makeTemporaryEnv config creator * fix: remove unused * docs: improve explanation Co-authored-by: Kristoffer K. <merceyz@users.noreply.github.com> * fix: escape ` Co-authored-by: Kristoffer K. <merceyz@users.noreply.github.com> (cherry picked from commit 5681a5f)
1 parent b90147c commit d582568

File tree

5 files changed

+64
-7
lines changed

5 files changed

+64
-7
lines changed

.yarn/versions/efc24e59.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
releases:
2+
"@yarnpkg/cli": minor
3+
"@yarnpkg/plugin-essentials": minor
4+
5+
declined:
6+
- "@yarnpkg/plugin-compat"
7+
- "@yarnpkg/plugin-constraints"
8+
- "@yarnpkg/plugin-dlx"
9+
- "@yarnpkg/plugin-init"
10+
- "@yarnpkg/plugin-interactive-tools"
11+
- "@yarnpkg/plugin-nm"
12+
- "@yarnpkg/plugin-npm-cli"
13+
- "@yarnpkg/plugin-pack"
14+
- "@yarnpkg/plugin-patch"
15+
- "@yarnpkg/plugin-pnp"
16+
- "@yarnpkg/plugin-pnpm"
17+
- "@yarnpkg/plugin-stage"
18+
- "@yarnpkg/plugin-typescript"
19+
- "@yarnpkg/plugin-version"
20+
- "@yarnpkg/plugin-workspace-tools"
21+
- "@yarnpkg/builder"
22+
- "@yarnpkg/core"
23+
- "@yarnpkg/doctor"

packages/acceptance-tests/pkg-tests-specs/sources/commands/add.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,23 @@ describe(`Commands`, () => {
215215
}),
216216
);
217217

218+
test(
219+
`it should not upgrade the existing dependency in the current project for preferReuse`,
220+
makeTemporaryEnv({
221+
devDependencies: {
222+
[`no-deps`]: `1.0.0`,
223+
},
224+
}, {preferReuse: true}, async ({path, run, source}) => {
225+
await run(`add`, `no-deps`);
226+
227+
await expect(xfs.readJsonPromise(`${path}/package.json` as PortablePath)).resolves.toMatchObject({
228+
devDependencies: {
229+
[`no-deps`]: `1.0.0`,
230+
},
231+
});
232+
}),
233+
);
234+
218235
test(
219236
`it should add a new peer dependency to the current project`,
220237
makeTemporaryEnv({}, async ({path, run, source}) => {

packages/gatsby/static/configuration/yarnrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,12 @@
695695
"type": "boolean",
696696
"default": false
697697
},
698+
"preferReuse": {
699+
"_package": "@yarnpkg/plugin-essentials",
700+
"description": "If true, `yarn add` will attempt to reuse the most common dependency range in other workspaces.",
701+
"type": "boolean",
702+
"default": false
703+
},
698704
"preferTruncatedLines": {
699705
"_package": "@yarnpkg/core",
700706
"description": "If true, Yarn will truncate lines that would go beyond the size of the terminal. If progress bars are disabled, lines will never be truncated. Forgettable lines (e.g. the fetch step logs) are always truncated.",

packages/plugin-essentials/sources/commands/add.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,23 @@ export default class AddCommand extends BaseCommand {
128128
});
129129

130130
const interactive = this.interactive ?? configuration.get(`preferInteractive`);
131+
const reuse = interactive || configuration.get(`preferReuse`);
131132

132133
const modifier = suggestUtils.getModifier(this, project);
133134

134135
const strategies = [
135-
...interactive ? [
136-
suggestUtils.Strategy.REUSE,
137-
] : [],
136+
reuse ?
137+
suggestUtils.Strategy.REUSE
138+
: undefined,
139+
138140
suggestUtils.Strategy.PROJECT,
139-
...this.cached ? [
140-
suggestUtils.Strategy.CACHE,
141-
] : [],
141+
142+
this.cached ?
143+
suggestUtils.Strategy.CACHE
144+
: undefined,
145+
142146
suggestUtils.Strategy.LATEST,
143-
];
147+
].filter((strategy): strategy is suggestUtils.Strategy => typeof strategy !== `undefined`);
144148

145149
const maxResults = interactive
146150
? Infinity

packages/plugin-essentials/sources/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ declare module '@yarnpkg/core' {
108108
// One in packages/plugin-essentials and one virtual package.
109109
// Defining this property with two different enum instances leads to a compiler error.
110110
defaultSemverRangePrefix: `^` | `~` | ``;
111+
preferReuse: boolean;
111112
}
112113
}
113114

@@ -125,6 +126,12 @@ const plugin: Plugin = {
125126
values: [`^`, `~`, ``],
126127
default: suggestUtils.Modifier.CARET,
127128
},
129+
130+
preferReuse: {
131+
description: `If true, \`yarn add\` will attempt to reuse the most common dependency range in other workspaces.`,
132+
type: SettingsType.BOOLEAN,
133+
default: false,
134+
},
128135
},
129136
commands: [
130137
cleanCache,

0 commit comments

Comments
 (0)