Skip to content

Commit 53e2599

Browse files
authored
feat(node-workspace): maintain current version range prefix (#1723)
* feat(node-workspace): maintain current version range prefix * chore: added tests * chore: refactor * chore: review comments
1 parent b707d07 commit 53e2599

3 files changed

Lines changed: 83 additions & 9 deletions

File tree

__snapshots__/node-workspace.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Release notes for path: node1, releaseType: node
1515
* update dependency foo/bar to 1.2.3
1616
* The following workspace dependencies were updated
1717
* dependencies
18-
* @here/pkgA bumped from 3.3.3 to ^3.3.4
18+
* @here/pkgA bumped from 3.3.3 to 3.3.4
1919
</details>
2020
2121
<details><summary>@here/pkgC: 1.1.2</summary>
@@ -41,7 +41,7 @@ exports['NodeWorkspace plugin run appends dependency notes to an updated module
4141
* update dependency foo/bar to 1.2.3
4242
* The following workspace dependencies were updated
4343
* dependencies
44-
* @here/pkgA bumped from 3.3.3 to ^3.3.4
44+
* @here/pkgA bumped from 3.3.3 to 3.3.4
4545
`
4646

4747
exports['NodeWorkspace plugin run appends dependency notes to an updated module 4'] = `
@@ -98,6 +98,16 @@ Release notes for path: node1, releaseType: node
9898
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
9999
`
100100

101+
exports['NodeWorkspace plugin run respects version prefix 1'] = `
102+
{
103+
"name": "@here/plugin1",
104+
"version": "4.4.4",
105+
"peerDependencies": {
106+
"@here/pkgA": "^3.3.3"
107+
}
108+
}
109+
`
110+
101111
exports['NodeWorkspace plugin run should ignore peer dependencies 1'] = `
102112
:robot: I have created a release *beep* *boop*
103113
---

src/plugins/node-workspace.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,17 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
167167
for (const [depName, resolved] of graphPackage.localDependencies) {
168168
const depVersion = updatedVersions.get(depName);
169169
if (depVersion && resolved.type !== 'directory') {
170+
const currentVersion = this.combineDeps(pkg)?.[depName];
171+
const prefix = currentVersion
172+
? this.detectRangePrefix(currentVersion)
173+
: '';
170174
updatedPackage.updateLocalDependency(
171175
resolved,
172176
depVersion.toString(),
173-
'^'
177+
prefix
174178
);
175179
this.logger.info(
176-
`${pkg.name}.${depName} updated to ^${depVersion.toString()}`
180+
`${pkg.name}.${depName} updated to ${prefix}${depVersion.toString()}`
177181
);
178182
}
179183
}
@@ -315,11 +319,7 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
315319
allPackages.map(packageJson => packageJson.name)
316320
);
317321
for (const packageJson of allPackages) {
318-
const allDeps = Object.keys({
319-
...(packageJson.dependencies ?? {}),
320-
...(packageJson.devDependencies ?? {}),
321-
...(packageJson.optionalDependencies ?? {}),
322-
});
322+
const allDeps = Object.keys(this.combineDeps(packageJson));
323323
const workspaceDeps = allDeps.filter(dep =>
324324
workspacePackageNames.has(dep)
325325
);
@@ -343,6 +343,31 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
343343
protected pathFromPackage(pkg: Package): string {
344344
return pkg.location;
345345
}
346+
347+
private detectRangePrefix(version: string): string {
348+
return (
349+
Object.values(SUPPORTED_RANGE_PREFIXES).find(supportedRangePrefix =>
350+
version.startsWith(supportedRangePrefix)
351+
) || ''
352+
);
353+
}
354+
355+
private combineDeps(packageJson: Package): Record<string, string> {
356+
return {
357+
...(packageJson.dependencies ?? {}),
358+
...(packageJson.devDependencies ?? {}),
359+
...(packageJson.optionalDependencies ?? {}),
360+
};
361+
}
362+
}
363+
364+
enum SUPPORTED_RANGE_PREFIXES {
365+
CARET = '^',
366+
TILDE = '~',
367+
GREATER_THAN = '>',
368+
LESS_THAN = '<',
369+
EQUAL_OR_GREATER_THAN = '>=',
370+
EQUAL_OR_LESS_THAN = '<=',
346371
}
347372

348373
function getChangelogDepsNotes(original: Package, updated: Package): string {

test/plugins/node-workspace.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,45 @@ describe('NodeWorkspace plugin', () => {
143143
assertHasUpdate(updates, 'node1/package.json');
144144
snapshot(dateSafe(nodeCandidate!.pullRequest.body.toString()));
145145
});
146+
it('respects version prefix', async () => {
147+
const candidates: CandidateReleasePullRequest[] = [
148+
buildMockCandidatePullRequest('plugin1', 'node', '4.4.4', {
149+
component: '@here/plugin1',
150+
updates: [
151+
buildMockPackageUpdate(
152+
'plugin1/package.json',
153+
'plugin1/package.json'
154+
),
155+
],
156+
}),
157+
buildMockCandidatePullRequest('node1', 'node', '2.2.2', {
158+
component: '@here/pkgA',
159+
updates: [
160+
buildMockPackageUpdate('node1/package.json', 'node1/package.json'),
161+
],
162+
}),
163+
];
164+
plugin = new NodeWorkspace(github, 'main', {
165+
plugin1: {releaseType: 'node'},
166+
node1: {releaseType: 'node'},
167+
});
168+
const newCandidates = await plugin.run(candidates);
169+
expect(newCandidates).lengthOf(1);
170+
const nodeCandidate = newCandidates.find(
171+
candidate => candidate.config.releaseType === 'node'
172+
);
173+
expect(nodeCandidate).to.not.be.undefined;
174+
const updates = nodeCandidate!.pullRequest.updates;
175+
assertHasUpdate(updates, 'node1/package.json');
176+
177+
const update = assertHasUpdate(
178+
updates,
179+
'plugin1/package.json',
180+
RawContent
181+
);
182+
const updater = update.updater as RawContent;
183+
snapshot(updater.rawContent);
184+
});
146185
it('combines node packages', async () => {
147186
const candidates: CandidateReleasePullRequest[] = [
148187
buildMockCandidatePullRequest('.', 'node', '5.5.6', {

0 commit comments

Comments
 (0)