Skip to content

Commit

Permalink
fix(release): use prepatch version for pre-release dependent package …
Browse files Browse the repository at this point in the history
…updates (#29123)
  • Loading branch information
fabioatcorreia authored Dec 9, 2024
1 parent 5d61191 commit 3474d7c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
64 changes: 64 additions & 0 deletions packages/js/src/generators/release-version/release-version.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,70 @@ To fix this you will either need to add a package.json file at that location, or
}
`);
});

it('should update dependents with a prepatch when creating a pre-release version', async () => {
expect(readJson(tree, 'libs/my-lib/package.json').version).toEqual(
'0.0.1'
);
expect(
readJson(
tree,
'libs/project-with-dependency-on-my-pkg/package.json'
).version
).toEqual('0.0.1');
expect(
readJson(
tree,
'libs/project-with-devDependency-on-my-pkg/package.json'
).version
).toEqual('0.0.1');

await releaseVersionGenerator(tree, {
projects: Object.values(projectGraph.nodes), // version all projects
projectGraph,
currentVersionResolver: 'disk',
specifier: 'prepatch',
preid: 'alpha',
releaseGroup: createReleaseGroup('independent'),
});

expect(readJson(tree, 'libs/my-lib/package.json'))
.toMatchInlineSnapshot(`
{
"name": "my-lib",
"version": "0.0.2-alpha.0",
}
`);

expect(
readJson(
tree,
'libs/project-with-dependency-on-my-pkg/package.json'
)
).toMatchInlineSnapshot(`
{
"dependencies": {
"my-lib": "0.0.2-alpha.0",
},
"name": "project-with-dependency-on-my-pkg",
"version": "0.0.2-alpha.0",
}
`);
expect(
readJson(
tree,
'libs/project-with-devDependency-on-my-pkg/package.json'
)
).toMatchInlineSnapshot(`
{
"devDependencies": {
"my-lib": "0.0.2-alpha.0",
},
"name": "project-with-devDependency-on-my-pkg",
"version": "0.0.2-alpha.0",
}
`);
});
});
});
});
Expand Down
25 changes: 21 additions & 4 deletions packages/js/src/generators/release-version/release-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ import {
import { sortProjectsTopologically } from './utils/sort-projects-topologically';
import { updateLockFile } from './utils/update-lock-file';

function resolvePreIdSpecifier(currentSpecifier: string, preid?: string) {
if (!currentSpecifier.startsWith('pre') && preid) {
return `pre${currentSpecifier}`;
}

return currentSpecifier;
}

export async function releaseVersionGenerator(
tree: Tree,
options: ReleaseVersionGeneratorSchema
Expand Down Expand Up @@ -89,7 +97,10 @@ Valid values are: ${validReleaseVersionPrefixes

// Set default for updateDependents
const updateDependents = options.updateDependents ?? 'auto';
const updateDependentsBump = 'patch';
const updateDependentsBump = resolvePreIdSpecifier(
'patch',
options.preid
) as ReleaseType;

// Sort the projects topologically if update dependents is enabled
// TODO: maybe move this sorting to the command level?
Expand Down Expand Up @@ -445,8 +456,13 @@ To fix this you will either need to add a package.json file at that location, or
);
} else {
let extraText = '';
if (options.preid && !specifier.startsWith('pre')) {
specifier = `pre${specifier}`;
const prereleaseSpecifier = resolvePreIdSpecifier(
specifier,
options.preid
);

if (prereleaseSpecifier !== specifier) {
specifier = prereleaseSpecifier;
extraText = `, combined with your given preid "${options.preid}"`;
}
logger.buffer(
Expand Down Expand Up @@ -598,6 +614,7 @@ To fix this you will either need to add a package.json file at that location, or
options.releaseGroup.projectsRelationship === 'independent'
);

// list of projects that depend on the current package
const allDependentProjects = Object.values(localPackageDependencies)
.flat()
.filter((localPackageDependency) => {
Expand Down Expand Up @@ -765,7 +782,7 @@ To fix this you will either need to add a package.json file at that location, or
dependentProject: LocalPackageDependency;
dependencyPackageName: string;
newDependencyVersion: string;
forceVersionBump: 'major' | 'minor' | 'patch' | false;
forceVersionBump: ReleaseType | false;
}) => {
const updatedFilePath = joinPathFragments(
projectNameToPackageRootMap.get(dependentProject.source),
Expand Down

0 comments on commit 3474d7c

Please sign in to comment.